5、NSValue、NSNumber、NSDictionary
.h
#import <Foundation/Foundation.h>
@interface Person : NSObject
{
NSString *_name;
NSInteger _age;
}
- (void)setName:(NSString *)name;
- (NSString *)name;
- (void)setAge:(NSInteger)age;
- (NSInteger)age;
- (instancetype)initWithName:(NSString *)name age:(NSInteger)age;
+ (instancetype)personWithName:(NSString *)name age:(NSInteger)age;
@end
.m
#import "Person.h"
@implementation Person
- (void)setName:(NSString *)name
{
_name = name;
}
- (NSString *)name
{
return _name;
}
- (void)setAge:(NSInteger)age
{
_age = age;
}
- (NSInteger)age
{
return _age;
}
- (instancetype)initWithName:(NSString *)name age:(NSInteger)age
{
self = [super init];
if (self) {
_name = name;
_age = age;
}
return self;
}
+ (instancetype)personWithName:(NSString *)name age:(NSInteger)age
{
return [[Person alloc] initWithName:name age:age];
}
- (NSString *)description
{
return [NSString stringWithFormat:@"name = %@ age = %ld", _name, _age];
}
@end
main.m
#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *name = @"生气大炮哥";
char gender = 'f';
int age = 19;
BOOL b = YES;
float score = 78.5;
NSNumber *genderNumber1 = [[NSNumber alloc] initWithChar:gender];
NSNumber *ageNumber = [NSNumber numberWithInt:age];
NSNumber *bNumber = [NSNumber numberWithBool:b];
NSNumber *scoreNumber = [NSNumber numberWithFloat:score];
NSMutableArray *mutableArray = [NSMutableArray arrayWithObjects:ageNumber, bNumber, scoreNumber, genderNumber1, name, nil];
NSLog(@"%@", mutableArray);
NSNumber *ageNumber2 = mutableArray[0];
int age3 = [ageNumber2 intValue];
age3 += 1;
NSRange range = NSMakeRange(10, 20);
NSValue *rangeObject = [NSValue valueWithRange:range];
NSLog(@"%@", rangeObject);
NSRange range2 = [rangeObject rangeValue];
NSUInteger l = range2.length;
NSLog(@"%lu", l);
NSDictionary *dict = @{
@"key": @"value",
@"k2": @"v2",
@"k3": @"v3"
};
NSLog(@"%@", dict[@"k2"]);
NSLog(@"%@", dict[@"k211"]);
NSDictionary *dict2 = [[NSDictionary alloc] initWithObjects:@[@"a", @"b"] forKeys:@[@"1", @"2"]];
NSLog(@"%@", dict2);
NSDictionary *dict3 = [[NSDictionary alloc] initWithObjectsAndKeys:@"a", @"b", @"c", @"d", nil];
NSLog(@"%@", dict3);
Person *p1 = [Person personWithName:@"同桌" age:23];
Person *p2 = [Person personWithName:@"前桌" age:24];
Person *p3 = [Person personWithName:@"后桌" age:19];
Person *p4 = [Person personWithName:@"隔壁桌" age:25];
NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
[mutableDict setObject:p1 forKey:@"tong"];
[mutableDict setObject:p2 forKey:@"qian"];
[mutableDict setObject:p3 forKey:@"hou"];
[mutableDict setObject:p4 forKey:@"gebi"];
NSLog(@"%@", mutableDict);
NSLog(@"%@", [p1 description]);
Person *p5 = [Person personWithName:@"gg" age:34];
[mutableDict setObject:p5 forKey:@"gebi"];
NSLog(@"%@", mutableDict);
[mutableDict removeObjectForKey:@"gebi"];
NSLog(@"%@", mutableDict);
[mutableDict setDictionary:@{
@"key": @"value"
}];
NSLog(@"%@", mutableDict);
NSMutableDictionary *mutableDict1 = [NSMutableDictionary dictionaryWithObjects:@[@"v1", @"v2"] forKeys:@[@"k1", @"k2"]];
for (int i = 0; i < mutableDict1.count; i++) {
NSString *key = mutableDict1.allKeys[i];
NSString *value = mutableDict1[key];
NSLog(@"key = %@ value = %@", key, value);
}
NSSet *set1 = [NSSet setWithObjects:@"a", @"b", @"a", nil];
NSLog(@"%@", set1);
NSArray *array = @[@"12", @"34"];
NSSet *set2 = [NSSet setWithArray:array];
NSLog(@"%@", set2);
NSLog(@"%lu", set1.count);
NSMutableSet *mutableSet = [NSMutableSet set];
[mutableSet addObject:@"aa"];
[mutableSet addObjectsFromArray:@[@"aa", @"bb"]];
NSLog(@"%@", mutableSet);
[mutableSet removeObject:@"aa"];
NSLog(@"%@", mutableSet);
NSArray *ages = @[@12, @13, @34, @12, @12, @34, @34.5, @12];
NSCountedSet *agesSet = [NSCountedSet setWithArray:ages];
NSUInteger count = [agesSet countForObject:@12];
NSLog(@"%lu", count);
for (NSNumber *num in agesSet) {
NSLog(@"%@", num);
}
}
return 0;
}