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

Could not signal service com.apple.WebKit.WebContent: 113: Could not find specified service

接手一个项目,Debug运行后打开WKWebView的页面,Xcode报出日志 Could not signal service com.apple.WebKit.WebContent: 113: Could not find specified service 解决办法: 在WKWebView在Window上显示之前,不要加载网页 直白的代码如下: [self.view addSubView:wkwebView]; [wkwebView loadRequest:request]

November 7, 2019 · 1 min · holdsky

WKWebView:适配H5页面的显示尺寸

一般的同样的html代码,在UIWebView和WKWebView中显示效果是不一样的。例如 <!DOCTYPE html> <html> <body> <span>test_input</span> <input id="test_input" type="text"> </body> </html> 显示效果: 可以看出两者有明显的区别。 那么如何适配呢?有两个方法(本质上是一个)。 1、如果是H5适配WKWebView的话,需要在H5添加如下代码 <meta name="viewport" content="width=device-width"> 完整的html如下 <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width"> </head> <body> <span>test_input</span> <input id="test_input" type="text"> </body> </html> 2、如果是WKWebView适配H5的话,那么就需要替前端添加相同的H5代码,实现如下 //js代码,在文档加载完毕后,添加head的子节点 //节点内容为<meta name="viewport" content="width=device-width"> let jScript = "var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);"; let wkUScript = WKUserScript.init(source: jScript, injectionTime: WKUserScriptInjectionTime.atDocumentEnd, forMainFrameOnly: true); let wkUController = WKUserContentController.init(); wkUController.addUserScript(wkUScript); let wkconfig = WKWebViewConfiguration.init(); wkconfig.userContentController = wkUController; _webView = WKWebView.init(frame: CGRect.init(x: 0, y: 0, width: self.view.bounds.size.width, height: self.view.bounds.size.height), configuration: wkconfig); self.view.addSubview(_webView); let htmlfile = Bundle.main.path(forResource: "test", ofType: ".html"); let url = URL.init(fileURLWithPath: htmlfile!); let request = URLRequest.init(url: url); _webView.load(request); 参考 https://blog.csdn.net/GYMotgm/article/details/77944163

November 4, 2019 · 1 min · holdsky

卸载Xcode的命令

sudo rm -rf /Applications/Xcode.app sudo rm -rf /Library/Preferences/com.apple.dt.Xcode.plist rm -rf ~/Library/Preferences/com.apple.dt.Xcode.plist rm -rf ~/Library/Caches/com.apple.dt.Xcode rm -rf ~/Library/Application\ Support/Xcode rm -rf ~/Library/Developer/Xcode rm -rf ~/Library/Developer/CoreSimulator rm -rf ~/Library/Developer/XCPGDevices

November 1, 2019 · 1 min · holdsky