天天看點

iOS Foundation 基礎架構NSObject 基類2. 參考

基本的函數的使用,不多做介紹,照着敲一遍代碼就明白了。。

NSObject 基類

1. NSString 字元串類

1. NSSttring

1. C 風格字元串和 OC 風格字元串之間的轉換

#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        char *str1 = "a string";
        NSString *str2 = [NSString stringWithUTF8String:str1];
        NSLog(@"str2 = %@", str2);        
        NSString *str3 = @"string";
        NSLog(@"str4 = %s", [str3 UTF8String]);
    }
    return ;
}
           

2. 建立字元串

1. 系統幫我們執行個體化對象和釋放記憶體
NSString *str = @"string";



           
2. 需要手動釋放記憶體
NSString *str = [[NSString alloc] init];
NSString *str = @"string";


           

3. 格式化字元串

#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        int a = ;
        int b = ;
        NSString *str = [NSString stringWithFormat:@"a = %d b = %d", a, b];
        NSLog(@"str = %@", str);
    }
    return ;
}
           

4. 拼接字元串

#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString *str1 = @"string1";
        NSString *str2 = @"string2";
        NSString *str3 = [str1 stringByAppendingString:str2];
        NSLog(@"str3 = %@", str3);
    }
    return ;
}
           

5. 大小寫轉換

#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString *str1 = @"STRing";
        NSString *str2 = [str1 lowercaseString];
        NSLog(@"str2 = %@", str2);
        NSString *str3 = [str1 uppercaseString];
        NSLog(@"str3 = %@", str3);
    }
    return ;
}
           

6. 字元串字首、字尾

#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString *str = @"www.google.com";
        BOOL hasPreFix = [str hasPrefix:@"www"];
        if (hasPreFix)
            NSLog(@"has prefix");
        else
            NSLog(@"not have prefix");
        BOOL hasSufFix = [str hasSuffix:@"com"];
        if (hasSufFix)
            NSLog(@"has suffix");
        else
            NSLog(@"not have suffix");
    }
    return ;
}
           

7. 字元串相等性判斷

#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString *str1 = @"www.apple.com";
        NSString *str2 = @"www.aple.com";
        if ([str1 isEqualToString:str2])
            NSLog(@"equal");
        else
            NSLog(@"not equal");
    }
    return ;
}
           

8. 字元串比較

NSComparisonResult 方法

9. 字元串分割

#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString *str1 = @"a, b, c, d";
        NSArray *strArray = [str1 componentsSeparatedByString:@", "];
        for( NSString *str in strArray )
        {
            NSLog(@"str = %@", str);
        }
    }
    return ;
}
           

10. 字元串截取

#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString *str = @"0123456789";
        NSRange range = NSMakeRange(, );
        NSString *str1 = [str substringWithRange:range];
        NSLog(@"str1 = %@", str1);
        NSString *str2 = [str substringFromIndex:];
        NSLog(@"str2 = %@", str2);
        NSString *str3 = [str substringToIndex:];
        NSLog(@"str3 = %@", str3);
    }
    return ;
}
           

11. 字元串拆分

#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString *str = @"0123456789";
        for (int i = ; i < [str length]; i++)
        {
            NSLog(@"%c", [str characterAtIndex:i]);
        }
    }
    return ;
}
           

12. 字元串查找

#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString *str = @"ab cd ef cd gh ijk";
        NSRange range = [str rangeOfString:@"cd"];
        NSLog(@"range.location:%ld range.length:%ld", range.location, range.length);        
    }
    return ;
}
           

13. 字元串替換

#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString *str = @"hello there, hello here";
        NSString *str1 = [str stringByReplacingCharactersInRange:NSMakeRange(, ) withString:@"你好"];
        NSLog(@"str1 = %@", str1);
        NSString *str2 = [str stringByReplacingOccurrencesOfString:@"hello" withString:@"你好"];
        NSLog(@"str2 = %@", str2);
    }
    return ;
}
           

14. 讀取檔案

使用 NSURL 路徑類讀取網絡和本地檔案

#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString *str = @"http://blog.csdn.net/username";
        NSURL *httpURL = [NSURL URLWithString:str];
        NSURL *fileURL = [NSURL fileURLWithPath:str];
        NSString *httpStr = [NSString stringWithContentsOfURL:httpURL encoding:NSUTF8StringEncoding error:nil];
        NSLog(@"httpStr = %@", httpStr);
        NSString *fileStr = [NSString stringWithContentsOfFile:@"/Users/username/Documents/test.txt" encoding:NSUTF8StringEncoding error:nil];
        NSLog(@"fileStr = %@", fileStr);
    }
    return ;
}
           

15. 寫入檔案

#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString *str = @"write file";
        BOOL rtn = [str writeToURL:@"/Users/username/Documents/demo.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
        if (rtn)
            NSLog(@"write success");
        else
            NSLog(@"write fail");
    }
    return ;
}
           

2. NSMutableString 可變字元串

可變字元串是字元串類的子類,是以,字元串類的方法可以用于可變字元串,反之則不行。

1. 追加字元串

#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSMutableString *str = [[NSMutableString alloc] initWithCapacity:];
        [str setString:@"hello"];
        [str appendString:@" world"];
        int a = ;
        [str appendFormat:@" %d", a];
        NSLog(@"str = %@", str);
    }
    return ;
}
           

2. 替換字元串

#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSMutableString *str = [[NSMutableString alloc] initWithCapacity:];
        [str setString:@"hello world"];
        NSRange range = [str rangeOfString:@"world"];
        [str replaceCharactersInRange:range withString:@"iOS"];
        NSLog(@"str = %@", str);
    }
    return ;
}
           

3. 插入字元串

#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSMutableString *str = [[NSMutableString alloc] initWithCapacity:];
        [str setString:@"hello world"];
        [str insertString:@"a " atIndex:];
        NSLog(@"str = %@", str);
    }
    return ;
}
           

4. 删除字元串

#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSMutableString *str = [[NSMutableString alloc] initWithCapacity:];
        [str setString:@"hello world"];
        NSRange range = [str rangeOfString:@"ello"];
        [str deleteCharactersInRange:range];
        NSLog(@"str = %@", str);
    }
    return ;
}
           

2. 數組

數組元素隻能是對象

同一個數組可以存儲不同類型的對象

記憶體空間依然連續

不論是什麼對象,數組元素都是一個指針

1. NSArray

1. 基本操作

  1. 數組初始化
  2. 數組長度
  3. 數組元素判斷
  4. 數組最後一個元素
  5. 數組第一個元素
  6. 數組元素查找
  7. 數組元素位置
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSArray *array = [[NSArray alloc] initWithObjects:@"1", @"2", @"3", @"4", @"5", nil];
        int count = (int) array.count;
        NSLog(@"count = %d", count);
        BOOL ishas = [array containsObject:@"2"];
        if (ishas)
        {
            NSLog(@"contain");
        }
        else
        {
            NSLog(@"not contain");
        }
        NSString *str = [array lastObject];
        NSLog(@"str = %@", str);
        str = [array firstObject];
        NSLog(@"str = %@", str);
        str = [array objectAtIndex:];
        NSLog(@"str = %@", str);
        int index = (int) [array indexOfObject:@"3"];
        NSLog(@"index = %d", index);
    }
    return ;
}
           

2. 數組的周遊

  1. for 循環
  2. for in 快速枚舉(需要數組元素類型一緻)
  3. 枚舉器(疊代器)
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSArray *array = [[NSArray alloc] initWithObjects:@"1", @"2", @"3", @"4", @"5", nil];
        for (int i = ; i < array.count; i++)
        {
            NSString *str1 = [array objectAtIndex:i];
            NSLog(@"array[%d] = %@", i, str1);
        }
        for (NSString *str2 in array)
        {
            NSLog(@"array = %@", str2);
        }
        NSEnumerator *enumerator;
        enumerator = [array objectEnumerator];
        id thing;
        while (thing = [enumerator nextObject]) {
            NSLog(@"%@",thing);
        }
    }
    return ;
}
           

2. Multable Array 可變數組

1. 添加和删除

addObject

addObjectsFromArray

removeAllObjects

removeLastObject

removeOjbect

removeOjbectAtIndex

2. 交換數組元素位置

exchangeObjectAtIndex:(NSUInteger) withObjectAtIndex:(NSUInteger)

3. 字典

1. NSDictionary

存儲記憶體空間不連續

使用鍵(key)值(value)對(KVC, key value coding, 鍵值編碼)

注意

  1. 添加或删除數組或字典的元素時,count 會随之變化
  2. 對象釋放後,每個值都會 release
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSDictionary *dict1 = [NSDictionary dictionaryWithObject:@"1" forKey:@"a"];
        NSLog(@"dict1 = %@", dict1);
        NSDictionary *dict2 = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"1", @"2", @"3", nil] forKeys:[NSArray arrayWithObjects:@"a", @"b", @"c", nil]];
        NSLog(@"dict2 = %@", dict2);
        NSDictionary *dict3 = @{@"a":@"1", @"b":@"2", @"c":@"3"};
        NSLog(@"dict3 = %@", dict3);
        int count = (int)[dict2 count];
        NSLog(@"count = %d", count);
        NSString *value1 = [dict2 valueForKey:@"b"];
        NSLog(@"value1 = %@", value1);
        NSString *value2 = [dict2 objectForKey:@"b"];
        NSLog(@"value2 = %@", value2);
        NSArray *allValues = [dict2 allValues];
        NSLog(@"allValues = %@", allValues);
        NSArray *allKeys = [dict2 allKeys];
        NSLog(@"allKeys = %@", allKeys);
        NSArray *array = [dict2 objectsForKeys:[NSArray arrayWithObjects:@"a", @"b", @"d", nil] notFoundMarker:@"not found"];
        NSLog(@"array = %@", array);
        for (NSString *key in dict2)
        {
            NSLog(@"%@ = %@", key, [dict2 objectForKey:key]);
        }
        NSEnumerator *en = [dict2 keyEnumerator];
        id key = nil;
        while (key = [en nextObject]) {
            NSLog(@"key = %@", key);
        }
    }
    return ;
}
           

2. NSMutbaleDictionary

#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
        [dict setObject:@"1" forKey:@"a"];
        [dict setObject:@"2" forKey:@"b"];
        [dict setObject:@"3" forKey:@"c"];
        [dict setObject:@"4" forKey:@"d"];
        NSLog(@"dict = %@", dict);
        [dict removeObjectForKey:@"a"];
        NSLog(@"dict = %@", dict);
        [dict removeObjectsForKeys:[NSArray arrayWithObjects:@"b", @"d", nil]];
        NSLog(@"dict = %@", dict);
        [dict removeAllObjects];
        NSLog(@"dict = %@", dict);
    }
    return ;
}
           

2. 參考

iOS 基礎入門之 Foundation 架構初體驗