丁保国的博客

收集整理工作生活,点点滴滴

  :: :: 博问 :: 闪存 :: :: :: :: 管理 ::

在iOS学习过程中,我们需要用到很多基本数据类型,我们先学习Foundation Kit,Foundation Kit是Cocoa中比较重要的组件。

首先在你的代码中要包含下面这句话,然后在程序中,才可以引用。

 1 #import <Foundation/Foundation.h> 

 

1. 字符串

NSString

NSString *height;
height = [NSString stringWithFormat:@"Your height is %d feet, %d inches", 5, 11];

字符串比较

NSString *thing1 = @"hello 5";
NSString *thing2 = [NSString stringWithFormat: @"hello %d", 5];

if([thing1 isEqualToString: thing2])
{
  NSLog(@"The strings are the same!");
}

if(thing1 == thing2)
{
    NSLog(@"They are the same object!");
}

 不区分大小写比较

if([thing1 compare: thing2 options: NSCaseInsensitiveSearch | NSNumericSearch] == NSOrderedSame)
{
    NSLog(@"They match!");
}

Contains

        NSString *fileName = @"draft-chapter.pages";
        
        if([fileName hasPrefix:@"draft"])
        {
            NSLog(@"this is a draft");
        }
        
        if([fileName hasSuffix:@".mov"])
        {
            NSLog(@"this is a movie");
        }
        
        if([fileName containsString:@"chapter"])
        {
            NSLog(@"this is a chapter");
        }

 NSString same with java's String, NSMutableString same with Java's StringBuffer.

        NSMutableString *friends = [NSMutableString stringWithCapacity:50];
        [friends appendString:@"James BethLynn Jack Evan"];
        
        NSRange jackRange = [friends rangeOfString:@"Jack"];
        jackRange.length++; //eat the space that follows
        
        [friends deleteCharactersInRange:jackRange];
        
        NSLog(friends);

 

 

2. 集合大家族

Array

     NSArray *array = [NSArray arrayWithObjects:@"one", @"two", @"three", nil]; //That is why we cannot use nil as a element
        
        //the other way
        NSArray *array2 = @[@"one", @"two", @"three"];
        
        for (NSInteger i = 0; i < [array count]; i++) {
            NSLog(@"index %d has %@.", i, [array objectAtIndex:i]);
        }
        
        for (NSInteger i = 0; i < [array2 count]; i++) {
            NSLog(@"index %d has %@.", i, array2[i]);
        }
        
        //Split String to Array
        NSString *string = @"oop:ack:bork:greeble:ponies";
        NSArray *chunks = [string componentsSeparatedByString:@":"];

        for (NSInteger i = 0; i < [chunks count]; i++) {
            NSLog(@"index %d has %@.", i, chunks[i]);
        }

 NSMutableArray

        NSMutableArray *array = [NSMutableArray arrayWithCapacity:17];
        for (NSInteger i = 0; i < 4; i++) {
            NSObject *o = [NSObject new];
            [array addObject:o];
        }

 

NSEnumeratoer

        NSEnumerator *enumerator = [array objectEnumerator];
        while (id thingie = [enumerator nextObject]) {
            NSLog(@"I found %@", thingie);
        }
        
        for (NSString *string in array) {
            NSLog(@"I found %@", string);
        }

 

 

3. 其他数值

NSNumber

NSValue

NSNull

posted on 2015-06-22 22:36  丁保国  阅读(170)  评论(0编辑  收藏  举报