//数字对象的创建与使用 //main.m #import<Foundation/Foundation.h> int main(int argc, const char argv[]) { @autoreleasepool{ //创建一个NSNumber“数字对象” NSNumber *monthNumber = [NSNumber numberWithInt:10]; NSNumber *lengthNumber = [[NSNumber alloc] initWithFloat:10.8]; //....... NSLog(@"monthNumber = %@", monthNumber); NSLog(@"lengthNumber = %@", lengthNumber); //输出:monthNumber = 10;lengthNumber = 10.8 //还原成基本数据类型 int month = [monthNumber intValue]; float length = [lengthNumber floatValue]; //....... NSLog(@"month:%d", month); NSLog(@"length:%.1f",length); //输出:month = 10;length = 10.8 } return 0; }
//字符串的创建与使用 #import<Foundation/Foundation.h> int main(int argc, const char argv[]) { @autoreleasepool{ //创建一个字符串常量 NSString *string = @"..."; //创建一个空的字符串 NSString *string = [[NSString alloc] init]; NSString *string = [NSString string]; //快速创建一个字符串 NSString *string = [[NSString alloc] initWithString:@"..."]; NSString *string = [NSString stringWithString:@"..."]; //快速创建一个格式化字符串 int number = 2; NSString *string = [[NSString alloc] initWithFormat:@"%d", number]; //测试字符串内容是否相同 if([string1 isEqualToString:string2]) { }; //测试两个字符串是否为同一对象 if(string1 == string2){}; //
cocoa foundation笔记-1
时间: 2024-10-16 10:05:12