ios小知识点(1)

最近在做的项目用到了下面的知识点,做个简单的归纳整理。仅做日常积累。
数组、字符串互相转换

有规律的分隔符会对数组、字符串帮助

// *字符串转数组 规律的
NSArray *domainArray = [t_domain componentsSeparatedByString:@","];
// *数组转字符串 规律的
NSString *arrayString = [array componentsJoinedByString:@","];
根据域名解析IP

下面是根据C对域名进行解析。常用的地方为网络性能检测。

// *引入头文件
#include 

+ (NSArray *)syncGetaddrinfoWithDomain:(NSString *)domain{
    if (domain.length == 0) {
        return nil;
    }
    struct addrinfo hints;
    memset(&hints, 0, sizeof(hints));
    hints.ai_family = PF_INET;
    hints.ai_protocol = IPPROTO_TCP;
    struct addrinfo *addrs, *addr;
    int getResult = getaddrinfo([domain UTF8String], NULL, &hints, &addrs);
    if (getResult || addrs == nil) {
        return nil;
    }
    addr = addrs;
    NSMutableArray *result = [NSMutableArray array];
    for (addr = addrs; addr; addr = addr->ai_next) {
        char host[NI_MAXHOST];
        memset(host, 0, NI_MAXHOST);
        getnameinfo(addr->ai_addr, addr->ai_addrlen, host, sizeof(host), NULL, 0, NI_NUMERICHOST);
        if (strlen(host) != 0) {
            [result addObject:[NSString stringWithUTF8String:host]];
        }
    }
    freeaddrinfo(addrs);
    return result;
}
取消延迟执行函数

取消延迟执行函数cancelPreviousPerformRequestsWithTarget

[[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(ceshi) object:nil];

你可能感兴趣的:(ios小知识点(1))