话说我打算一天学完object c语法,系列3--------来自书Objective-c程序设计

来源:互联网 发布:linux安装java 编辑:程序博客网 时间:2024/05/17 05:13

NSString中的几个常用方法:

isEqualToString、还可以通过compare来比较两个字符串的顺序,如果第一个字符串小于第二个则结果是NSOrderedAscending,NSorderedSame相等,NSOrderedDescending第一个字符串大于第二个、caseInsensitiveCompare(不想进行大小写的敏感检查)、uppedcaseString、lowercaseString

字符串截取:

str2 = [str1 substringToIndex:5],如果str1为"Rangeor index out of bounds",则str2为Range

字符串查找:

rangngeOfString

str2 = [str1rangngeOfString:@"Sam"]; str2.location可以获得位置信息


NSString是不可修改的字符串,二NSMutableString是可修改的字符串




数组对象

不可变数组(NSArray)

在数组中的元素必须以nil结束,NSArray数组类上的方法有:


来个例子

////  main.m//  test////  Created by Zeng on 13-5-24.//  Copyright (c) 2013年 zeng. All rights reserved.//#import <Foundation/Foundation.h>int main(int argc, const char * argv[]){    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];     NSArray *city = [NSArray arrayWithObjects:@"上海", @"广州", @"宁波", @"杭州", @"重庆", @"武汉", nil];        for (int i = 0; i < [city count]; i++) {        NSLog(@"%@", [city objectAtIndex:i]);    }        [pool release];    return 0;} 

常用的方法:



可变数组(NSMutableArray),他是NSArray的子类

对应于NSString和NSMutableString,NSMutableArray是可以动态管理数组的

常用方法有:


来个例子

使用sortUsingSelector+NSMutableArray再来一个例子

////  main.m//  test////  Created by Zeng on 13-5-24.//  Copyright (c) 2013年 zeng. All rights reserved.//#import <Foundation/Foundation.h>#import "Student.h"int main(int argc, const char * argv[]){    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];     Student *stu1 = [[Student alloc] init];    Student *stu2 = [[Student alloc] init];    Student *stu3 = [[Student alloc] init];        [stu1 setName:@"Sam"];    [stu1 setAge:30];    [stu2 setName:@"Lee"];    [stu2 setAge:33];    [stu3 setName:@"Alex"];    [stu3 setAge:36];        NSMutableArray *students = [[NSMutableArray alloc] init];    [students addObject:stu1];    [students addObject:stu2];    [students addObject:stu3];        NSLog(@"排序前");    for (int i = 0; i < [students count]; i++) {        Student *stu4 = [students objectAtIndex:i];        NSLog(@"Name : %@, Age :%i", [stu4 name], [stu4 age]);    }        [students sortUsingSelector:@selector(compareName:)];        NSLog(@"排序后");    for (Student *stu4 in students) {        NSLog(@"Name : %@, Age : %i", [stu4 name], [stu4 age]);    }        [students release];    [stu3 release];    [stu2 release];    [stu1 release];    [pool release];    return 0;}

常用方法:



字典对象(NSDictionary和NSMutableDictionary)

NSDictionary的作用同java中的字典类相同,提供了“键-值”对的集合

他的方法有:


例子:

////  main.m//  test////  Created by Zeng on 13-5-24.//  Copyright (c) 2013年 zeng. All rights reserved.//#import <Foundation/Foundation.h>#import "Student.h"int main(int argc, const char * argv[]){    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];     NSDictionary *employees = [NSDictionary dictionaryWithObjectsAndKeys:@"曹操", @"1", @"孙权", @"2", @"刘备", @"3", nil];        NSString *firstEmployee = [employees objectForKey:@"1"];    NSLog(@"1 key for name is : %@", firstEmployee);        [pool release];    return 0;} 

同样的他也有可变的版本NSMutableDictionary:


例子呢:

////  main.m//  test////  Created by Zeng on 13-5-24.//  Copyright (c) 2013年 zeng. All rights reserved.//#import <Foundation/Foundation.h>int main(int argc, const char * argv[]){    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];     NSMutableDictionary *student = [NSMutableDictionary dictionary];        [student setObject:@"历史上的曹操性格非常复杂,陈寿认为曹操在历史上明略最优,揽申、商之法术,该韩、白之奇策,官方授材,各因其器,矫情任算,不念旧恶" forKey:@"曹操"];    [student setObject:@"诸葛亮治国治军的才能,济世爱民,谦虚谨慎的品格为后世各种杰出任务树立了榜样" forKey:@"诸葛亮"];    [student setObject:@"陈寿对刘备的评价是:“弘毅宽厚,知人待士,盖有高祖之风,英雄之器焉。及其举国托孤于诸葛亮,而心神无二,诚君臣之至公,古今之盛轨也。机权干略,不逮威武,是以基宇亦狭”" forKey:@"刘备"];        NSLog(@"曹操:%@", [student objectForKey:@"曹操"]);    NSLog(@"诸葛亮:%@", [student objectForKey:@"诸葛亮"]);    NSLog(@"刘备:%@", [student objectForKey:@"刘备"]);            [pool release];    return 0;}

他们俩的常用方法:




集合对象(NSSet)

集合对象是一组单值对象的组合,比如1个包含1到50个数字的集合。集合对象的操作包括搜索、添加、删除集合中的成员(可变集合的功能),比较两个集合,计算两个集合的交集和并集等

一个例子P199

////  main.m//  test////  Created by Zeng on 13-5-24.//  Copyright (c) 2013年 zeng. All rights reserved.//#import <Foundation/Foundation.h>@interface NSSet (printInteger)-(void) printSet;@end@implementation NSSet (printInteger)-(void) printSet{    for (NSNumber *integer in self) {        printf("%li ", [integer integerValue]);    }    printf("\n");}@endint main(int argc, const char * argv[]){    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];        NSMutableSet *set1 = [NSMutableSet setWithObjects:[NSNumber numberWithInteger:1], [NSNumber numberWithInteger:3], [NSNumber numberWithInteger:5], nil];        NSMutableSet *set2 = [NSMutableSet setWithObjects:[NSNumber numberWithInteger:2], [NSNumber numberWithInteger:4], [NSNumber numberWithInteger:4], [NSNumber numberWithInteger:6], nil];        if ([set1 isEqualToSet:set2] == YES) {        NSLog(@"set1 == set2");    }else{       NSLog(@"set1 != set2");     }        if ([set1 containsObject:set2] == YES) {        NSLog(@"set1 包含 set2");    }else{        NSLog(@"set1 不包含 set2");    }        [set1 printSet];        [set1 addObject:[NSNumber numberWithInteger:6]];    [set1 removeObject:[NSNumber numberWithInteger:1]];        [set1 printSet];        [set1 intersectSet:set2];    [set1 printSet];        [set1 unionSet:set2];    [set1 printSet];        [pool release];    return 0;}

    在上面的程序中,演示了一些机会的常用方法,并未NSSet类添加了一个名为pintInteger的category。因为可变集合NSMutableSet是NSSet类的子类,所以NSMutableSet也拥有这个方法。




访问数组、字典和集合

方法1:

使用count和objectAtIndex:id

for(int i = 0; i < count; i++){member = [array objecyAtIndex:i];NSLog([member description]);}

方法2,使用快速枚举,有点像foreach:

for(Member *member in array){ NSLog([member description]);}

一个字段快速枚举的例子:

////  main.m//  test////  Created by Zeng on 13-5-24.//  Copyright (c) 2013年 zeng. All rights reserved.//#import <Foundation/Foundation.h>int main(int argc, const char * argv[]){    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];        NSMutableDictionary *student = [NSMutableDictionary dictionary];    [student setObject:@"历史上的曹操性格非常复杂,陈寿认为曹操在历史上明略最优,揽申、商之法术,该韩、白之奇策,官方授材,各因其器,矫情任算,不念旧恶" forKey:@"曹操"];    [student setObject:@"诸葛亮治国治军的才能,济世爱民,谦虚谨慎的品格为后世各种杰出任务树立了榜样" forKey:@"诸葛亮"];    [student setObject:@"陈寿对刘备的评价是:“弘毅宽厚,知人待士,盖有高祖之风,英雄之器焉。及其举国托孤于诸葛亮,而心神无二,诚君臣之至公,古今之盛轨也。机权干略,不逮威武,是以基宇亦狭”" forKey:@"刘备"];        for (NSString *key in student) {        NSLog(@"%@ : %@", key, [student objectForKey:key]);    }        [student release];    [pool release];    return 0;}