记录一些 Objective-C 的实用代码片段,本篇属于个人笔记

常用关键字一览

1
2
3
4
5
6
@interface、@implementation、@end 
@public@protected@private@selector
@try@catch@throw@finally
@protocol、@optional、@required、@class
@property@synthesize@dynamic
BOOL Class SEL YES NO id self super nil atomic nonatomic retain assign copy block …

方法调用 基础语法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#import <Foundation/Foundation.h>

// - 表示动态方方法
// +表示静态方法
@interface TestClass : NSObject
{
@public
//下划线开头
NSString * _name;
int _age;
}

@property NSString * NickName;

+(void) staticSayHello;
-(void) SayHello;

+(double) staticTestWithParams:(int) num ThisIsWord:(int) num2;

@end

//实现上文中的方法才可以调用
@implementation TestClass

//实现构造函数
-(id)init
{
if (self = [super init]) {
NSLog(@"初始化构造函数...");
}
returnself;
}
-(id)initWithNickname:(NSString*)nickName
{
if (self = [super init]) {
NSLog(@"带有参数的构造 %@",nickName);
}
returnself;
}
-(id)initAgeAndNickname:(int)age andName :(NSString*)name
{
if (self =[super init]) {
NSLog(@"带有双参数的构造 %d +++ %@",age,name);
}
returnself;
}


-(void) SayHello
{
NSLog(@"Name is %@ and age is %d",_name,_age);
}
+(void)staticSayHello{
NSLog(@"Static Call SayHello must call by Class ..");
}
+(double)staticTestWithParams:(int)num ThisIsWord:(int) num2
{
NSLog(@"%d +========+++=%d",num,num2);
return num;
}
@end

int main(int argc, const char * argv[]) {

TestClass* test = [[TestClassalloc] initAgeAndNickname:21andName:@"xiaonian"];

test->_name = @"Hello World";
test->_age = 21;

[test SayHello];//Call Method
[TestClassstaticSayHello];//Casll Static Method
[TestClassstaticTestWithParams:123456ThisIsWord:890123];

NSLog(@"--------分隔符---------");

Class c = [TestClass class];
NSLog(@"%p",c);
//0x100004708

NSLog(@"--------分隔符-@SEL--------");
SEL sel = @selector(SayHello);
[test performSelector:sel];
[test performSelector:@selector(SayHello)];
if ([test respondsToSelector:sel]) {
NSLog(@"%@",@"可以相应sel--sayhello");
}
NSLog(@"--------分隔符-@property--------");

[test setNickName:@"xiaonian"];
NSString* nickName = [test NickName];
test.NickName = @"xiaonian2";
NSString* nickName2 = test.NickName;
NSLog(@"%@ and %@",nickName,nickName2);

return 0;
}

基础类型转换

1
2
3
4
5
6
7
8
9
10
11
12
int hours = floor(rawtime / 3600000);

int mins = floor((rawtime % 3600000) / (1000 * 60));
int secs = floor(((rawtime % 3600000) % (1000 * 60)) / 1000);

NSLog(@"%d:%d:%d", hours, mins, secs);

NSString *hoursStr = [NSString stringWithFormat:@"%d", hours];
NSString *minsStr = [NSString stringWithFormat:@"%d", mins];
NSString *secsStr = [NSString stringWithFormat:@"%d", secs];

NSLog(@"%a:%a:%a", hoursStr, minsStr, secsStr);

参考链接 int to NSString

获取设备当前网络IP地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//获取设备当前网络IP地址
- (NSString *)getIPAddress:(BOOL)preferIPv4
{
NSArray *searchArray = preferIPv4 ?
@[ /*IOS_VPN @"/" IP_ADDR_IPv4, IOS_VPN @"/" IP_ADDR_IPv6,*/ IOS_WIFI @"/" IP_ADDR_IPv4, IOS_WIFI @"/" IP_ADDR_IPv6, IOS_CELLULAR @"/" IP_ADDR_IPv4, IOS_CELLULAR @"/" IP_ADDR_IPv6 ] :
@[ /*IOS_VPN @"/" IP_ADDR_IPv6, IOS_VPN @"/" IP_ADDR_IPv4,*/ IOS_WIFI @"/" IP_ADDR_IPv6, IOS_WIFI @"/" IP_ADDR_IPv4, IOS_CELLULAR @"/" IP_ADDR_IPv6, IOS_CELLULAR @"/" IP_ADDR_IPv4 ] ;

NSDictionary *addresses = [self getIPAddresses];
NSLog(@"addresses: %@", addresses);

__block NSString *address;
[searchArray enumerateObjectsUsingBlock:^(NSString *key, NSUInteger idx, BOOL *stop)
{
address = addresses[key];
if(address) *stop = YES;
} ];
return address ? address : @"0.0.0.0";
}

//获取所有相关IP信息
- (NSDictionary *)getIPAddresses
{
NSMutableDictionary *addresses = [NSMutableDictionary dictionaryWithCapacity:8];

// retrieve the current interfaces - returns 0 on success
struct ifaddrs *interfaces;
if(!getifaddrs(&interfaces)) {
// Loop through linked list of interfaces
struct ifaddrs *interface;
for(interface=interfaces; interface; interface=interface->ifa_next) {
if(!(interface->ifa_flags & IFF_UP) /* || (interface->ifa_flags & IFF_LOOPBACK) */ ) {
continue; // deeply nested code harder to read
}
const struct sockaddr_in *addr = (const struct sockaddr_in*)interface->ifa_addr;
char addrBuf[ MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) ];
if(addr && (addr->sin_family==AF_INET || addr->sin_family==AF_INET6)) {
NSString *name = [NSString stringWithUTF8String:interface->ifa_name];
NSString *type;
if(addr->sin_family == AF_INET) {
if(inet_ntop(AF_INET, &addr->sin_addr, addrBuf, INET_ADDRSTRLEN)) {
type = IP_ADDR_IPv4;
}
} else {
const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6*)interface->ifa_addr;
if(inet_ntop(AF_INET6, &addr6->sin6_addr, addrBuf, INET6_ADDRSTRLEN)) {
type = IP_ADDR_IPv6;
}
}
if(type) {
NSString *key = [NSString stringWithFormat:@"%@/%@", name, type];
addresses[key] = [NSString stringWithUTF8String:addrBuf];
}
}
}
// Free memory
freeifaddrs(interfaces);
}
return [addresses count] ? addresses : nil;
}