2013年5月26日 星期日

Objective-C unsigned int ( NSUInteger )

輸出 NSUInteger,格式指定符為 %u 或是 %lu (long unsigned)
取得字串長度:
NSString *city = @"Taichung";
NSUInteger cityLength = [city length];
NSLog(@"City has %u characters", cityLength)
result:
City has 8 characters

數學運算:
NSNumber *price = [NSNumber numberWithInt: 20];
NSNumber *amount = [NSNumber numberWithInt: 6];

NSUInteger priceInt = [price unsignedIntValue];
NSUInteger amountInt = [amount unsignedIntValue];
NSUInteger total = priceInt * amountInt;

NSLog(@"Total price is %lu", total);
result:
Total price is 120
NSUInteger 的宣告無 * 符號

輸出 NSInteger,格式指定符為 %d 或是 %ld (long),取值訊息為 intValue

沒有留言:

張貼留言