OC常用类:NSArray、NSDictionary、NSData
1、NSArray
2、NSDictionary
3、NSData
NSArray
NSArray
#import <Foundation/Foundation.h>
#import "ClassA.h"
#import "ClassB.h"
int main(int argc, const char * argv[])
{
ClassA * a = [[ClassA alloc] init];
a.x = 1;
a.y = 2;
ClassB * b = [[ClassB alloc] init];
b.a = 3;
b.b = 4;
NSArray * arr = [NSArray arrayWithObjects:a, b, nil];
//向数组中所有对象发送消息。@selector(方法名),前提是所有对象有这个方法
[arr makeObjectsPerformSelector:@selector(print)];
NSLog(@"%@", arr);
return 0;
}
数组排序
NSArray(普通的数组排序)
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
NSString * str1 = @"aa";
NSNumber * int1 = [NSNumber numberWithInt:11];
NSString * str2 = @"BB";
NSArray * arr = [NSArray arrayWithObjects:str1, int1, nil];
id x1 = [arr objectAtIndex:1];
long count = [arr count];
long x2 = [arr indexOfObject:int1];
for (int i = 0; i < count; i++)
{
NSLog(@"%@", [arr objectAtIndex:i]);
}
for (id object in arr)
{
NSLog(@"%@", object);
}
NSArray * arr1 = [NSArray arrayWithObjects:str1, str2, @"11", nil];
NSArray * arr2 = [arr1 sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"%@", arr2);
return 0;
}
NSArray 数组排序之自写comparePerson
#import <Foundation/Foundation.h>
#import "ClassA.h"
int main(int argc, const char * argv[])
{
ClassA * a1 = [ClassA personWithAge:18 withName:@"aa"];
ClassA * a2 = [ClassA personWithAge:20 withName:@"aa"];
ClassA * a3 = [ClassA personWithAge:18 withName:@"bb"];
ClassA * a4 = [[ClassA alloc] initWithAge:22 withName:@"aa"];
NSArray * arr = [NSArray arrayWithObjects:a1, a2, a3, a4, nil];
NSArray * sarr = [arr sortedArrayUsingSelector:@selector(comparePerson:)];
NSLog(@"%@", sarr);
#pragma mark --block语法sortedArrayUsingComparator:
- (NSArray *)sortedArrayUsingComparator:(NSComparator)cmptr
typedef NS_ENUM(NSInteger, NSComparisonResult) {NSOrderedAscending = -1L, NSOrderedSame, NSOrderedDescending};
NSComperator compareStringsBlock = ^(id stringA, id stringB) {
NSRange rangeS = NSMakeRange (0, [stringA length]);
return (stringA compare:stringB options:comparisonOptions range:rangeS locale:currentLocale];};
NSArray *array = [NSArray arrayWithObjects:a1, a2, a3, a4, nil];
NSArray *sortedArray = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2){NSComparisonResult result = [obj1 comparePerson:obj2];return result;}];
NSLog(@"%@",sortedArray);
这里的代码可以参照上面compare:默认的排序方法,也可以把自定义的方法写在这里,给对象排序
[a4 release];
return 0;
}
ClassA.h
#import <Foundation/Foundation.h>
@interface ClassA : NSObject
@property (nonatomic) int x, y;
- (void) print;
#pragma mark --自写compare
@property (nonatomic)int age;
@property (nonatomic, retain) NSString * name;
+(id)personWithAge:(int) age withName:(NSString *)name;
-(id)initWithAge:(int) age withName:(NSString *)name;
-(NSComparisonResult)comparePerson:(ClassA *)person;
-(NSString *) description;
@end
ClassA.m
#import "ClassA.h"
@implementation ClassA
@synthesize x, y;
- (void) print
{
NSLog(@"%d%d", x, y);
}
#pragma mark --自写comparePerson
@synthesize age, name;
+(id)personWithAge:(int) age withName:(NSString *)name
{
ClassA *person = [[ClassA alloc] init];
person.age = age;
person.name = name;
return person;
}
-(id)initWithAge:(int)_age withName:(NSString *)_name;
{
if (self = [super init])
{
age = _age;
name = _name;
}
return self;
}
-(NSComparisonResult)comparePerson:(ClassA *)person
{
//默认按年龄排序
NSComparisonResult result = [[NSNumber numberWithInt:self.age] compare:[NSNumber numberWithInt:person.age]];//注意:基本数据类型要进行数据转换
//如果年龄一样,就按照名字排序
if (result == NSOrderedSame) {
result = [self.name compare:person.name];
}
return result;
}
-(NSString *) description
{
return [NSString stringWithFormat:@"age:%d, name:%@", age, name];
}
@end
NSArray 数组排序之高级排序(组合类的排序,排序描述器sortedArrayUsingDescriptors:)
#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[])
{
Car * c1 = [Car carWithColor:@"white"];
Car * c2 = [Car carWithColor:@"black"];
Person * p1 = [Person personWithAge:18 withName:@"aa" withCar:c1];
Person * p2 = [Person personWithAge:18 withName:@"bb" withCar:c1];
Person * p3 = [Person personWithAge:20 withName:@"aa" withCar:c1];
Person * p4 = [Person personWithAge:20 withName:@"aa" withCar:c2];
NSArray *array = [NSArray arrayWithObjects:p1, p2, p3, p4, nil];
//构建排序描述器
NSSortDescriptor *carColorDesc = [NSSortDescriptor sortDescriptorWithKey:@"car.color" ascending:YES];
NSSortDescriptor *personNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
NSSortDescriptor *personAgeDesc = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES];
//把排序描述器放进数组里,放入的顺序就是你想要排序的顺序(age、name、color)
NSArray *descriptorArray = [NSArray arrayWithObjects:personAgeDesc,personNameDesc, carColorDesc, nil];
NSArray *sortedArray = [array sortedArrayUsingDescriptors: descriptorArray];
NSLog(@"%@",sortedArray);
return 0;
}
Car
#import <Foundation/Foundation.h>
@interface Car : NSObject
@property (nonatomic, retain) NSString * color;
+(Car *)carWithColor:(NSString *)_color;
@end
#import "Car.h"
@implementation Car
@synthesize color;
+(Car *)carWithColor:(NSString *)_color
{
Car *car = [[Car alloc] init];
car.color = _color;
return car;
}
@end
Person
#import <Foundation/Foundation.h>
#import "Car.h"
@interface Person : NSObject
@property (nonatomic)int age;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) Car * car;
+(id)personWithAge:(int) age withName:(NSString *)name withCar:(Car *)car;
-(NSString *) description;
@end
#import "Person.h"
@implementation Person
@synthesize age, name, car;
+(id)personWithAge:(int) age withName:(NSString *)name withCar:(Car *)car
{
Person *person = [[Person alloc] init];
person.age = age;
person.name = name;
person.car = car;
return person;
}
-(NSString *) description
{
return [NSString stringWithFormat:@"age:%d, name:%@, color:%@", age, name, car.color];
}
@end
快速遍历
NSArray
#import <Foundation/Foundation.h>
int main ()
{
NSArray * arr = [NSArray arrayWithObjects:@"a", @"b", @"1", nil];
NSString * s = [arr objectAtIndex:2];
long count = [arr count];
long index = [arr indexOfObject:@"a"];
for (int i = 0; i < count; i++)
{
NSLog(@"%@", [arr objectAtIndex:i]);
}
for (id obj in arr)
{
NSLog(@"%@", obj);
}
return 0;
}
NSMutableArray
NSMutableArray
#import <Foundation/Foundation.h>
int main ()
{
NSMutableArray * arr = [NSMutableArray arrayWithCapacity:5];
[arr addObject:@"a"];
[arr insertObject:[NSNull null] atIndex:1]; //超出范围或者加入空指针,会抛出异常
NSMutableArray * arr2 = [NSMutableArray arrayWithObjects:@"aa", @"bb", nil];
[arr2 insertObject:@"cc" atIndex:2]; //同样,超出范围或者加入空指针,会抛出异常
bool b = [arr containsObject:@"1"]; //查找XX是否存在
[arr replaceObjectAtIndex:1 withObject:@"b"];
[arr removeObjectAtIndex:1];
//向数组中的所有对象发送消息
[arr2 makeObjectsPerformSelector:@selector(description)]; //自己写的类才会调用description??
NSLog(@"%@, %@", arr, arr2);
return 0;
}
<pre name="code" class="objc"><span style="background-color: rgb(255, 255, 255);"><span style="font-size:18px;color:#ff6666;">NSDictionary</span></span>
//<pre name="code" class="objc">NSDictionary
#import <Foundation/Foundation.h>int main (){ NSDictionary * d1 = [NSDictionary dictionaryWithObjectsAndKeys:@"a", @"1", @"b", @"2", nil]; NSMutableDictionary * d2 = [NSMutableDictionary dictionaryWithDictionary:d1]; [d2 setObject:@"c" forKey:@"3"]; //[d1 setObject:@"c" forKey:@"3"]; //不可变,不能增加对象 NSString * s = [d1 objectForKey:@"2"]; long c = [d2 count]; NSNumber * n = [NSNumber numberWithLong:c]; [d2 setObject:s forKey:n]; NSArray * a1 = [d1 allKeys]; NSArray * a2 = [d2 allValues]; NSArray * a3 = [d2 keysSortedByValueUsingSelector:@selector(compare:)]; NSLog(@"%@, %@, %@, %@, %@", d1, d2, a1, a2, a3); return 0;}
<span style="background-color: rgb(255, 255, 255);"><span style="font-size:18px;color:#ff6666;">NSDictionary的空值</span></span>
<pre name="code" class="objc">#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
NSDictionary * d = [NSDictionary dictionaryWithObjectsAndKeys:[NSNull null], @"Tel", nil];
NSDictionary * dd = [NSDictionary dictionaryWithObjectsAndKeys:@"18300766850", @"Tel", nil];
id nul = [d objectForKey:@"Tel"];
if (nul == [NSNull null])
{
NSLog(@"没有Telphone");
}
else
{
NSLog(@"%@", nul);
}
return 0;
}