【fantomlife】

【道由心生<<>>万道始于足】

   ::  ::  ::  ::  :: 管理

1、- (NSUInteger)indexOfObject:(id)obj inSortedRange:(NSRange)r options:(NSBinarySearchingOptions)opts usingComparator:(NSComparator)cmp NS_AVAILABLE(10_6, 4_0); // binary search指定区域返回通过代码块方法的索引

NSUInteger amount = 9000;      
NSNumber* number = [NSNumber numberWithInt:724];
NSMutableArray* anArray = [NSMutableArray arrayWithCapacity:amount];
for (NSUInteger i = 0; i < amount; ++i) {;
    [anArray addObject:[NSNumber numberWithUnsignedInteger:i]];
}
NSUInteger index1 = [anArray indexOfObject:number
                             inSortedRange:NSMakeRange(0, [anArray count])
                                   options:NSBinarySearchingFirstEqual
                           usingComparator:^(id obj1,id obj2) {
                               NSInteger iVal1 = [obj1 integerValue];
                               NSInteger iVal2 = [obj2 integerValue];
                               if (iVal1 < iVal2)
                                   return NSOrderedAscending;
                               else if (iVal1 > iVal2)
                                   return NSOrderedDescending;
                               else
                                   return NSOrderedSame;
                           }];
NSLog(@"%lu", index1);
2013-03-10 18:49:52.302 NSStringDemo[396:303] 724

2、+ (id)array; 创建一个空数组
NSArray *demo = [NSArray array];
NSLog(@"%@", demo);
2013-03-10 18:51:40.546 NSStringDemo[439:303] (
)

3、+ (id)arrayWithObject:(id)anObject; 指定一个元素创建数组对象
NSLog(@"%@", [NSArray arrayWithObject:@"demo"]);
2013-03-10 18:53:09.747 NSStringDemo[482:303] (
    demo
)

4、+ (id)arrayWithObjects:(const id [])objects count:(NSUInteger)cnt;指定个数从一个数组对象创建新的数组对象(类消息)
demoString[0] = @"January";
demoString[1] = @"February";
demoString[2] = @"March";
NSArray *demoCount = [NSArray arrayWithObjects:demoString count:2];
NSLog(@"%@", [demoCount description]);


5、+ (id)arrayWithObjects:(id)firstObj, ... NS_REQUIRES_NIL_TERMINATION; 指定多个元素创建数组对象
NSArray *demos = [NSArray arrayWithObjects:@"demoOne", "demoTwo", @"demoThree", nil];
NSLog(@"%@",  demos[0]);

2013-03-10 19:01:47.060 NSStringDemo[700:303] (
    demo
)

6、+ (id)arrayWithArray:(NSArray *)array;生成新另外一个数组
NSLog(@"%@", [NSArray  arrayWithArray:demoCount]);
2013-03-10 19:16:29.434 NSStringDemo[985:303] (
    January,
    February
)

7、- (id)initWithObjects:(const id [])objects count:(NSUInteger)cnt;  指定个数从一个数组对象创建新的数组对象(实例消息)
NSLog(@"%@", [[NSArray alloc] initWithObjects:demoString count:2]);

2013-03-10 19:25:17.756 NSStringDemo[1218:303] (
    January,
    February
) 
8、- (id)initWithObjects:(id)firstObj, ... NS_REQUIRES_NIL_TERMINATION; 指定多个元素创建数组对象
NSArray *demoinitObjects = [[NSArray alloc] initWithObjects:@"demoOne", @"demoTwo", @"demoThree", nil];
NSLog(@"%@", [demoinitObjects description]);
2013-03-10 19:24:06.095 NSStringDemo[1195:303] (
    demoOne,
    demoTwo,
    demoThree
)

9、-(id)initWithArray:(NSArray *)array; 从已有数组初始化
NSLog(@"%@", [[[NSArray alloc] initWithArray:demos] autorelease]);
2013-03-10 19:26:33.091 NSStringDemo[1256:303] (
    demoOne,
    demoTwo,
    demoThree
)

10、- (id)initWithArray:(NSArray *)array copyItems:(BOOL)flag;从已有数组初始化(标示作用,浅复制,还是深复制)
NSLog(@"%@", [[NSArray alloc] initWithArray:demoCount copyItems:NO]);
2013-03-10 19:27:53.466 NSStringDemo[1295:303] (
    January,
    February
)

11、+ (id)arrayWithContentsOfFile:(NSString *)path;文件加载数据

12、+ (id)arrayWithContentsOfURL:(NSURL *)url;
从网络地址加载数据

13、- (id)initWithContentsOfFile:(NSString *)path;
文件加载数据

14、- (id)initWithContentsOfURL:(NSURL *)url;
//从网络地址加载数据

NSString *writeFilePath = @"/Users/fantom/work/ios/NSStringDemo/demo.xml";
NSURL *urlPath = [[NSURL alloc] initFileURLWithPath:[writeFilePath stringByExpandingTildeInPath]];
NSLog(@"%@",[NSArray arrayWithContentsOfFile:writeFilePath]);
NSLog(@"%@",[NSArray arrayWithContentsOfURL:urlPath]);
NSLog(@"%@",[[[NSArray alloc] initWithContentsOfFile:writeFilePath] autorelease]);
NSLog(@"%@",[[[NSArray alloc] initWithContentsOfURL:urlPath] autorelease]);


013-03-10 19:38:47.703 NSStringDemo[1701:303] (
    three,
    two,
    two
)
2013-03-10 19:38:47.706 NSStringDemo[1701:303] (
    three,
    two,
    two
)
2013-03-10 19:38:47.707 NSStringDemo[1701:303] (
    three,
    two,
    two
)
2013-03-10 19:38:47.708 NSStringDemo[1701:303] (
    three,
    two,
    two
)

posted on 2013-03-10 06:25  fantomlife  阅读(173)  评论(0)    收藏  举报