Objective-C 网络对时的简单实现

头文件 // // ZLNetworkTime.h // // Created by zxs.zl on 2018/3/12. // #import <Foundation/Foundation.h> @interface ZLNetworkTime : NSObject +(void)startSyncTimeFromNetwork; +(BOOL)syncTimeFromNetworkDone; +(NSTimeInterval)timeIntervalSince1970; @end 实现文件 // // ZLNetworkTime.m // // Created by zxs.zl on 2018/3/12. // #if ! __has_feature(objc_arc) #error This file must be compiled with ARC. Either turn on ARC for the project or use -fobjc-arc flag #endif #import "ZLNetworkTime.h" struct _ZLTimeBase_ { _ZLTimeBase_() { init_state = not_init; init_count = 0; } NSTimeInterval systemUptimeBase; NSTimeInterval networkTimBbase; enum { not_init, initing, inited, } init_state; int init_count; }s_timeBase; @implementation ZLNetworkTime +(NSTimeInterval)timeIntervalSince1970 { if (s_timeBase.init_state == s_timeBase.inited) return s_timeBase.networkTimBbase + [[NSProcessInfo processInfo] systemUptime] - s_timeBase.systemUptimeBase; else return [[NSDate date] timeIntervalSince1970]; } +(BOOL)syncTimeFromNetworkDone { return s_timeBase.init_state == s_timeBase.inited; } +(void)startSyncTimeFromNetwork { if (s_timeBase.init_state != s_timeBase.not_init) return; if (s_timeBase.init_count >= 3) return; @synchronized(self){ if (s_timeBase.init_state != s_timeBase.not_init) return; s_timeBase.init_state = s_timeBase.initing; } s_timeBase.init_count++; NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration]; config.timeoutIntervalForRequest = 3; config.requestCachePolicy = NSURLRequestReloadIgnoringCacheData; NSURLSession * session = [NSURLSession sessionWithConfiguration:config]; NSURL * url = [NSURL URLWithString:@"http://time.tianqi.com/"]; NSTimeInterval systemUptime = [[NSProcessInfo processInfo] systemUptime]; NSURLSessionDataTask * dataTask = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse*)response; if ([httpResponse isKindOfClass:[NSHTTPURLResponse class]] && httpResponse.statusCode == 200 ) { NSString * dateStr = httpResponse.allHeaderFields[@"Date"]; if (dateStr.length > 0) { //Mon, 12 Mar 2018 07:21:32 GMT NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.timeZone = [NSTimeZone timeZoneWithName:@"GMT"]; [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]]; [formatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss 'GMT'"]; NSDate *date = [formatter dateFromString:dateStr]; if (date) { s_timeBase.systemUptimeBase = [[NSProcessInfo processInfo] systemUptime]; s_timeBase.networkTimBbase = [date timeIntervalSince1970] + s_timeBase.systemUptimeBase - systemUptime; s_timeBase.init_state = s_timeBase.init_state; return; } } } s_timeBase.init_state = s_timeBase.not_init; }]; [dataTask resume]; } @end

November 20, 2019 · 2 min · holdsky

sudo 运行sh脚本,Permission denied

运行一个sh脚本,以普通用户运行,提示 Permission denied,z再使用sudo运行,还是提示 Permission denied。 于是,我用 ls -all 打印脚本的属性 -rw-rw-rw-@ 1 zjon staff 209535797 11 19 15:18 abc.sh 原来是没有执行属性 x,那么就分配属性 chmod 0766 abc.sh

November 19, 2019 · 1 min · holdsky

iOS 推出指定的UIViewController

UINavigationController 提供的常用接口是push和pop,但都是操作最顶层的页面 topViewController,若要操作指定的页面,可以访问属性 viewControllers @implementation UINavigationController(zlAddition) - (void)zl_popTheViewController:(UIViewController *)viewController animated:(BOOL)animated { if (viewController.navigationController == self) { if (self.topViewController == viewController) { [self popViewControllerAnimated:animated]; } else { NSMutableArray *arr = [self.viewControllers mutableCopy]; if (arr.lastObject != viewController) { [arr removeObject:viewController]; [self setViewControllers:arr animated:animated]; } } } } @end

November 19, 2019 · 1 min · holdsky

iOS 判断指针指向的对象是否已经被释放

在Windows平台上,判断野指针的库函数有一些,如 IsBadReadPtr, IsBadWritePtr, AfxIsValidAddress 。 在iOS 的Object-C 的MRC时代,也有一个类似的方法 malloc_zone_from_ptr extern malloc_zone_t *malloc_zone_from_ptr(const void *ptr); /* Returns the zone for a pointer, or NULL if not in any zone. The ptr must have been returned from a malloc or realloc call. */ 返回zone上的指针,或者返回NULL表示不在任何zone上。 ptr参数必须是已经调用malloc或realloc返回值。

November 18, 2019 · 1 min · holdsky

xcode-select切换或者指定xcode命令行版本

切换xcode命令行版本 sudo xcode-select --switch /指定版本的xcode路径Xcode.app 恢复默认的xcode命令行版本 当使用 xcodebuild 莫名其妙的出问题时,可以考虑使用此命令恢复 sudo xcode-select -r 使用对话框安装命令行工具 sudo xcode-select --install

November 14, 2019 · 1 min · holdsky

C++ 判断机器的大小端

class __judge_little_endian { public: static inline bool isle() { static const int _a = 0xAABBCCDD; return *(unsigned char *)(&_a) == 0xDD; } }; 对于整数 0xAABBCCDD 大端机器 内存中的排布为: AA BB CC DD 小端机器 内存中的排布为: DD CC BB AA

November 12, 2019 · 1 min · holdsky

大小端转换的C++ 封装 :hton , ntol

使用C++的模板、自动类型推导技术 #ifndef HtoN_h #define HtoN_h #include <arpa/inet.h> #if __ANDROID__ #ifndef htonll #define htonll(x) htonq(x) #endif #ifndef ntohll #define ntohll(x) ntohq(x) #endif #endif namespace zxszl { template <typename T> T zl_hton (T t) { return t;} template <> inline uint16_t zl_hton<uint16_t> (uint16_t t) { return htons(t); } template <> inline int16_t zl_hton<int16_t> (int16_t t) { return htons(t); } template <> inline uint32_t zl_hton<uint32_t> (uint32_t t) { return htonl(t); } template <> inline int32_t zl_hton<int32_t> (int32_t t) { return htonl(t); } template <> inline uint64_t zl_hton<uint64_t> (uint64_t t) { return htonll(t); } template <> inline int64_t zl_hton<int64_t> (int64_t t) { return htonll(t); } template <typename T> T zl_ntoh (T t) { return t;} template <> inline uint16_t zl_ntoh<uint16_t> (uint16_t t) { return ntohs(t); } template <> inline int16_t zl_ntoh<int16_t> (int16_t t) { return ntohs(t); } template <> inline uint32_t zl_ntoh<uint32_t> (uint32_t t) { return ntohl(t); } template <> inline int32_t zl_ntoh<int32_t> (int32_t t) { return ntohl(t); } template <> inline uint64_t zl_ntoh<uint64_t> (uint64_t t) { return ntohll(t); } template <> inline int64_t zl_ntoh<int64_t> (int64_t t) { return ntohll(t); } class __judge_little_endian { public: static inline bool isle() { static const int _a = 0xAABBCCDD; return *(unsigned char *)(&_a) == 0xDD;} }; #define zl_rorder_s(x) \ ((__uint16_t)((((__uint16_t)(x) & 0xff00) >> 8) | \ (((__uint16_t)(x) & 0x00ff) << 8))) #define zl_rorder_l(x) \ ((__uint32_t)((((__uint32_t)(x) & 0xff000000) >> 24) | \ (((__uint32_t)(x) & 0x00ff0000) >> 8) | \ (((__uint32_t)(x) & 0x0000ff00) << 8) | \ (((__uint32_t)(x) & 0x000000ff) << 24))) #define zl_rorder_ll(x) \ ((__uint64_t)((((__uint64_t)(x) & 0xff00000000000000ULL) >> 56) | \ (((__uint64_t)(x) & 0x00ff000000000000ULL) >> 40) | \ (((__uint64_t)(x) & 0x0000ff0000000000ULL) >> 24) | \ (((__uint64_t)(x) & 0x000000ff00000000ULL) >> 8) | \ (((__uint64_t)(x) & 0x00000000ff000000ULL) << 8) | \ (((__uint64_t)(x) & 0x0000000000ff0000ULL) << 24) | \ (((__uint64_t)(x) & 0x000000000000ff00ULL) << 40) | \ (((__uint64_t)(x) & 0x00000000000000ffULL) << 56))) template <typename T> T zl_htole (T t) { return t;} template <> inline uint16_t zl_htole<uint16_t> (uint16_t t) {return __judge_little_endian::isle() ? t : zl_rorder_s(t);} template <> inline int16_t zl_htole<int16_t> (int16_t t) {return __judge_little_endian::isle() ? t : zl_rorder_s(t);} template <> inline uint32_t zl_htole<uint32_t> (uint32_t t) {return __judge_little_endian::isle() ? t : zl_rorder_l(t);} template <> inline int32_t zl_htole<int32_t> (int32_t t) {return __judge_little_endian::isle() ? t : zl_rorder_l(t);} template <> inline uint64_t zl_htole<uint64_t> (uint64_t t) { return __judge_little_endian::isle() ? t : zl_rorder_ll(t); } template <> inline int64_t zl_htole<int64_t> (int64_t t) { return __judge_little_endian::isle() ? t : zl_rorder_ll(t); } template <typename T> T zl_letoh (T t) { return t;} template <> inline uint16_t zl_letoh<uint16_t> (uint16_t t) {return __judge_little_endian::isle() ? t : zl_rorder_s(t);} template <> inline int16_t zl_letoh<int16_t> (int16_t t) {return __judge_little_endian::isle() ? t : zl_rorder_s(t);} template <> inline uint32_t zl_letoh<uint32_t> (uint32_t t) {return __judge_little_endian::isle() ? t : zl_rorder_l(t);} template <> inline int32_t zl_letoh<int32_t> (int32_t t) {return __judge_little_endian::isle() ? t : zl_rorder_l(t);} template <> inline uint64_t zl_letoh<uint64_t> (uint64_t t) { return __judge_little_endian::isle() ? t : zl_rorder_ll(t); } template <> inline int64_t zl_letoh<int64_t> (int64_t t) { return __judge_little_endian::isle() ? t : zl_rorder_ll(t); } #undef zl_rorder_s #undef zl_rorder_l #undef zl_rorder_ll } #endif /* HtoN_h */ 使用示例 ...

November 12, 2019 · 3 min · holdsky

React : Style prop value must be an object react/style-prop-object

我初学React,懵懵懂懂,我把基础的html语法直接搬到JSX文件中,React提示我: Line 17:36: Style prop value must be an object react/style-prop-object 我写的是: class CYXQHome extends React.Component { render(){ return ( <div id="showDiv1" style="display:none;position:relative;left:69px;width:360px" /> ); } } 出现这种错误的原因是 style=xxxx 那部分,style必须是一个对象 。修改后 <div id="showDiv1" style={ { display:"none",position:"relative",left:"69px",width:"360px" } } /> 如上,有两个花括号, {{ {} }},外部的 { {} } 表示这是Javascript句法,里面的 { {} } 是一个对象。 参考 http://www.mamicode.com/info-detail-1682242.html

November 11, 2019 · 1 min · holdsky

Xcode Run Script的执行有顺序

如题,顺序为 Build Phases 中的显示顺序

November 8, 2019 · 1 min · holdsky

使用__attribute __DATA 将常量写入Mach-O文件

1、 attribute DATA 宏 #define ZL_PULGIN_DATA(sectname) __attribute((used, section("__DATA,"#sectname" "))) #define ZL_PULGIN(key,value) \ const char * k##key##_##value##_zl ZL_PULGIN_DATA(ZLABCEDFG) = ""#key":"#value""; //ZLABCEDFG 可以自由定义,但不能超过8个字符 2、在实现文件如 .m 中使用 ZL_PULGIN(hello,world) 3、在运行时读取 读取函数 #include <mach-o/getsect.h> #include <mach-o/loader.h> #include <mach-o/dyld.h> #include <dlfcn.h> #import <objc/runtime.h> #import <objc/message.h> static NSArray<NSString *>* BHReadConfiguration(const char *section) { NSMutableArray *configs = [NSMutableArray array]; Dl_info info; dladdr(BHReadConfiguration, &info); #ifndef __LP64__ // const struct mach_header *mhp = _dyld_get_image_header(0); // both works as below line const struct mach_header *mhp = (struct mach_header*)info.dli_fbase; unsigned long size = 0; uint32_t *memory = (uint32_t*)getsectiondata(mhp, "__DATA", section, & size); #else /* defined(__LP64__) */ const struct mach_header_64 *mhp = (struct mach_header_64*)info.dli_fbase; unsigned long size = 0; uint64_t *memory = (uint64_t*)getsectiondata(mhp, "__DATA", section, & size); #endif /* defined(__LP64__) */ for(int idx = 0; idx < size/sizeof(void*); ++idx){ char *string = (char*)memory[idx]; NSString *str = [NSString stringWithUTF8String:string]; if(!str)continue; if(str) [configs addObject:str]; } return configs; } 执行结果 ...

November 8, 2019 · 1 min · holdsky