nsuinteger

iOS : NSInteger and NSUInteger comparison

对着背影说爱祢 提交于 2021-02-18 08:39:53
问题 Surprise! I've a variable like this, NSInteger index = 0; I'm comparing it with one of subviews count (which returns NSUInteger ) like this, if((index-1) <= [[currentmonth subviews] count]) { NSLog(@"true"); } else { NSLog(@"false"); } This always giving false. but If I'll do like this, if ((index-1) <= 42) { NSLog(@"true"); } else { NSLog(@"false"); } This always giving true. I feel that, this because we can't compare NSInteger with NSUInteger correct? I caught this issue, when I have a

Add NSUInteger to NSMutableArray

ε祈祈猫儿з 提交于 2020-01-21 03:43:07
问题 Hello I am working on a project and I am trying to add an NSUInteger to an NSMutableArray. I am new to Objective-C and C in general. When I run the app NSLog displays null. I'd appreciate any help anyone is able to provide. Here is my code -(NSMutableArray *)flipCardAtIndex:(NSUInteger)index { Card *card = [self cardAtIndex:index]; [self.flipCardIndexes addObject:index]; if(!card.isUnplayable) { if(!card.isFaceUp) { for(Card *otherCard in self.cards) { if(otherCard.isFaceUp && !otherCard

Convert NSInteger to NSUInteger?

空扰寡人 提交于 2019-12-31 12:06:22
问题 I am trying to convert a NSInteger to a NSUInteger and I googled it and found no real answer. How would I do this? 回答1: NSInteger and NSUInteger are just typedefs for primitive integer types: #if __LP64__ || NS_BUILD_32_LIKE_64 typedef long NSInteger; typedef unsigned long NSUInteger; #else typedef int NSInteger; typedef unsigned int NSUInteger; #endif As such, you don't need to "convert" between them. A simple cast should be sufficient. Like: NSInteger myInt = 0; NSUInteger unsignedInt =

NSUInteger in reversed loop confusion?

社会主义新天地 提交于 2019-12-20 06:26:52
问题 I wonder if someone can explain something, I setup a loop where I wanted to count backwards from 10 to 0 : for(NSUInteger index = 10; index >= 0; index--) { NSLog(@"INDEX: %ld", (long)index); } This loop runs forever, it does not stop at 0, but keeps going into negative numbers. When I noticed this I changed my code to : for(NSInteger index = 10; index >= 0; index--) { NSLog(@"INDEX: %ld", (long)index); } The above works fine, but I am curious, why the first example does not work as the

Is there a better way to avoid 'Sign comparison' warning when comparing NSIndexPath's row and NSArray count?

拟墨画扇 提交于 2019-12-10 13:13:05
问题 I turned 'Signed Comparision' (aka -Wsign-compare) warnings for my iOS project in XCode (surprisingly, it was off by default). After that lots of warnings like this appeared: /Users/michalciuba/projects/GlobeMobile/Classes/ACMailController.m:86:19: Comparison of integers of different signs: 'NSInteger' (aka 'long') and 'NSUInteger' (aka 'unsigned long') They are usually caused by comparing row property of NSIndexPath which is NSInteger to the value returned by 'count' method of NSArray , like

How do you convert an NSUInteger into an NSString?

有些话、适合烂在心里 提交于 2019-12-10 12:28:28
问题 How do you convert an NSUInteger into an NSString ? I've tried but my NSString returned 0 all the time. NSUInteger NamesCategoriesNSArrayCount = [self.NamesCategoriesNSArray count]; NSLog(@"--- %d", NamesCategoriesNSArrayCount); [NamesCategoriesNSArrayCountString setText:[NSString stringWithFormat:@"%d", NamesCategoriesNSArrayCount]]; NSLog(@"=== %d", NamesCategoriesNSArrayCountString); 回答1: When compiling with support for arm64 , this won't generate a warning: [NSString stringWithFormat:@"

Why NSInteger instead of NSUInteger in “numberOfSectionInTableView:”?

雨燕双飞 提交于 2019-12-06 23:36:54
问题 The UITableView data source method numberOfSectionsInTableView: has a return type of NSInteger . However, a UITableView cannot have a negative amount of rows; it has 0 or greater rows, so why is the return type of NSInteger? Doesn't that allow for crashes relating to a negative integer being returned? 回答1: You can't do the check (if var < 0) return; with an unsigned integer. That is the standard reason for preferring one. Really the only reason to use an unsigned integer is if you need the

Why NSInteger instead of NSUInteger in “numberOfSectionInTableView:”?

三世轮回 提交于 2019-12-05 04:02:04
The UITableView data source method numberOfSectionsInTableView: has a return type of NSInteger . However, a UITableView cannot have a negative amount of rows; it has 0 or greater rows, so why is the return type of NSInteger? Doesn't that allow for crashes relating to a negative integer being returned? You can't do the check (if var < 0) return; with an unsigned integer. That is the standard reason for preferring one. Really the only reason to use an unsigned integer is if you need the extra room for larger digits, and you can guarantee the input will never try to be less than zero. 来源: https:/

Convert NSInteger to NSUInteger?

那年仲夏 提交于 2019-12-02 23:55:44
I am trying to convert a NSInteger to a NSUInteger and I googled it and found no real answer. How would I do this? NSInteger and NSUInteger are just typedefs for primitive integer types: #if __LP64__ || NS_BUILD_32_LIKE_64 typedef long NSInteger; typedef unsigned long NSUInteger; #else typedef int NSInteger; typedef unsigned int NSUInteger; #endif As such, you don't need to "convert" between them. A simple cast should be sufficient. Like: NSInteger myInt = 0; NSUInteger unsignedInt = (NSUInteger)myInt; Since this might be useful for other coming across this issue here is a little table showing

NSUInteger in reversed loop confusion?

≡放荡痞女 提交于 2019-12-02 09:56:12
I wonder if someone can explain something, I setup a loop where I wanted to count backwards from 10 to 0 : for(NSUInteger index = 10; index >= 0; index--) { NSLog(@"INDEX: %ld", (long)index); } This loop runs forever, it does not stop at 0, but keeps going into negative numbers. When I noticed this I changed my code to : for(NSInteger index = 10; index >= 0; index--) { NSLog(@"INDEX: %ld", (long)index); } The above works fine, but I am curious, why the first example does not work as the numbers generated are all unsigned integers? An unsigned type can't "keep going into negative numbers".