標籤:
架構指objective-c的foundation庫,下面的例子中給出幾個常見用的類及其方法。
NSNumber *intNum; NSNumber *longNum; NSNumber *floatNum; intNum = [NSNumber numberWithInteger:12]; NSLog(@"%i", [intNum integerValue]); longNum = [NSNumber numberWithLong:0x123456]; NSLog(@"%lx", [longNum longLongValue]); floatNum = [NSNumber numberWithFloat:12.00]; NSLog(@"%f", [floatNum floatValue]); if([intNum isEqualToNumber:floatNum] == YES){ NSLog(@"eqaul"); //相同 }else{ NSLog(@"not equal"); }
NSString *str1 = @"hello,world"; NSString *str2 = [NSString stringWithFormat:@"%i,%@", 5, @"fredric"]; NSLog(@"%@%@", str1,str2); NSLog([str1 stringByAppendingString:str2]); NSMutableString *str3 = [NSMutableString stringWithString:@"hello"]; [str3 appendString:@"fredric_"]; [str3 insertString:@"word" atIndex:str3.length]; NSLog(@"%@",str3); //hellofredric_word NSRange res = [str3 rangeOfString:@"ric"]; if(res.location != NSNotFound){ [str3 deleteCharactersInRange:res]; } NSLog(@"%@",str3); //hellofred_word
NSArray *array = [NSArray arrayWithObjects:@"demo1",@"demo2",@"demo3", nil]; for(int i = 0; i < [array count]; i++){ NSLog(@"%@",[array objectAtIndex:i]); } NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:3]; [mArray addObject:@"demo4"]; [mArray addObject:@"demo5"]; [mArray addObject:@"demo6"]; for(int i = 0; i < [mArray count]; i++){ NSLog(@"%@",[mArray objectAtIndex:i]); }
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"value1",@"key1",@"value2",@"key2",@"valu3", @"key3", nil]; NSString *value1 = [dic objectForKey:@"key1"]; NSLog(@"%@", value1); NSMutableDictionary *mDic = [[NSMutableDictionary alloc]init]; [mDic setObject:@"value1_1" forKey:@"key1"]; NSLog(@"%@", [mDic objectForKey:@"key1"]);
objective-c(架構)