Tag: 核心基础

我怎么能打字检查CGColor / CGPath?

所以看起来有一个与CoreFoundationtypes有关的swift的提交错误 。 根据描述,看起来没有CGPath和CGColor的types检查,下面是从错误中演示的行为的片段。 func check(a: AnyObject) -> Bool { return a is CGColor } check("hey") –> true check(1.0) –> true check(UIColor.redColor()) –> true 这就是我想要做的 if let value = self.valueForKeyPath(keyPath) { if let currentValue = value as? CGColor { // Do Something with CGColor } else if let currentValue = value as? CGSize { // Do […]

CFHostGetAddressing()是否支持ipv6的DNS条目?

我正在尝试使用CFHostGetAddressing做一个简单的DNS查找。 但是,我注意到它返回了一个sockaddr结构体的数组,我猜这意味着它只能做IPV4。 有没有办法在iOS中支持IPV6地址的DNS条目? 也许一个类似的API返回一个sockaddr_storage结构数组?

iOS6上的CFAutoRelease()类行为

我有一个方法创build一个ABRecordRef,设置其属性并返回参考。 我在使用CFAutoRelease时遇到崩溃,因为我需要支持iOS <7。 我将如何正确地释放这个? -(ABRecordRef) myRecord{ ABRecordRef newRecord = ABPersonCreate(); //some setting here return CFAutoRelease(newRecord); //how to release here? }

如何使用CFNotificationCenterGetDarwinNotifyCenter()发送一个userInfo字典

我需要使用CFNotificationCenterGetDarwinNotifyCenter()发送一个对象,但是我注意到,每当我发送一个通知使用 const void *tkeys[1] = { @"testKey" }; const void *tvalues[1] = { @"testValue" }; CFDictionaryRef userInfo = CFDictionaryCreate(NULL, tkeys, tvalues, 1, NULL, NULL); CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), CFSTR("foo"), object, userInfo, true); 那么我发现在观察者callback中,userInfo字典是NULL。 有什么我在这里失踪? 有没有其他的方式,我可以发送userInfo对象? 我在看http://nshipster.com/inter-process-communication/和分布式通知部分,但是每当我使用: CFNotificationCenterRef distributedCenter = CFNotificationCenterGetDistributedCenter(); 那么每当我尝试它时,我都会收到错误“ Invalid Use of Function错误”。

ios编程调用,而不必退出应用程序

有没有任何方式来编程调用,而无需退出当前的应用程序? 我做了一个研究,但所有的答案都与 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]]; 这不是我想要的,我也需要访问语音stream。

消息发送到ARC使用自定义getter和setter释放实例

我试图为我的自定义对象HFObject实现自定义getter和setter,并且尽pipe使用ARC,我的应用程序崩溃了Message sent to deallocated instance错误的Message sent to deallocated instance 。 我已经阅读了每一个相关的文章,那些在ARC之前编写的文章并不适用,其他一切都没有帮助。 我打开了僵尸对象debugging器选项。 设置自定义HObject 在HObject.h中我已经声明了这四个属性: @property (retain) NSString *email; //Will use custom getter/setter @property (retain) NSString *firstName; //Will use custom getter/setter @property (retain) NSDate *date; //Will use custom getter/setter @property (nonatomic, retain) NSMutableDictionary *values; 在实施HObject的时候,我删除了email的自动获取和设置。 firstName和date这样利用@dynamic @dynamic email; @dynamic firstName; @dynamic date; 我也在HObject init中分配values字典 – (id)init […]

在Objective-C中,如何打印出N个空格? (使用stringWithCharacters)

以下是试图打印出N个空格(在本例中为12个): NSLog(@"hello%@world", [NSString stringWithCharacters:" " length:12]); const unichar arrayChars[] = {' '}; NSLog(@"hello%@world", [NSString stringWithCharacters:arrayChars length:12]); const unichar oneChar = ' '; NSLog(@"hello%@world", [NSString stringWithCharacters:&oneChar length:12]); 但是他们都打印出怪异的东西,比如hello ÔÅÓñüÔÅ®Óñü®ÓüÅ®ÓñüÔ®ÓüÔÅ®world ,我认为“字符数组”和“string”是一样的,和“指向字符的指针”是一样的, ? API规范说,它是一个“Unicode字符的C数组”(通过Unicode,是UTF8吗?如果是,那么它应该与ASCII兼容)…如何使它工作,为什么这3种方式赢得不工作?

在Swift中的CFDictionaryRef问题

我正在将一些旧的Objective-C代码转换成Swift,所以我可以从一些不赞成使用的方法转移,但我一直在崩溃,到目前为止我似乎无法弄清楚是什么导致了它。 我得到一个P12证书的私钥,这种方法似乎工作得很好,直到我真正需要从CFArray获取字典的部分,即使数组中有值的应用程序不断崩溃。 这里是我有的代码: func privateKeyFromCertificate(p12Name: String, withPassword password: String) -> SecKeyRef { let resourcePath: String = NSBundle.mainBundle().pathForResource(p12Name, ofType: "p12")! let p12Data: NSData = NSData(contentsOfFile: resourcePath)! let key : NSString = kSecImportExportPassphrase as NSString let options : NSDictionary = [key : password] var privateKeyRef: SecKeyRef? = nil var items : CFArray? let securityError: OSStatus = SecPKCS12Import(p12Data, […]

在swift中将NSData转换为sockaddr结构体

我正在试图做一个简单的DNS查询迅速。 到目前为止,这里是我有的代码: let hostRef = CFHostCreateWithName(kCFAllocatorDefault, "google.com").takeRetainedValue() var resolved = CFHostStartInfoResolution(hostRef, CFHostInfoType.Addresses, nil) let addresses = CFHostGetAddressing(hostRef, &resolved).takeRetainedValue() as NSArray 此时,“地址”NSArray中的每个元素都是包装sockaddr结构体的CFDataRef对象。 由于CFDataRef可以免费桥接到NSData,我可以像这样循环他们: for address: AnyObject in addresses { println(address) // address is of type NSData. } 到目前为止好(我认为)。 当我在unit testing中运行它时打印出有效的数据。 这里是我卡住的地方。 对于我来说,我不知道如何将NSData对象中的字节转换为sockaddr结构体。 我怎样才能将address.bytestypes的COpaquePointer?转换成ac结构? 任何帮助赞赏。 我正在试图弄清楚这一切,正在撞墙。

用__attribute __((NSObject))为CFtypes强的@property不保留

更新:这个问题已经修复Xcode 4.6! 这项技术现在再次按预期工作。 但是,在你使用代码之前,一定要阅读Rob Napier最佳答案的注释。 原来的post (ARC,Xcode 4.3.1,iOS 5.1) 我有一个CFtypes的强大的属性(CGImage),我想通过ARC使用__attribute__((NSObject))自动pipe理(如在保留和释放在合成的setter中,并且它在dealloc中是无效的),但它不起作用:当我分配属性时不保留该对象。 一个最小的例子来重现: @interface TestClass : NSObject @property (nonatomic, strong) __attribute__((NSObject)) CFStringRef str; @end // …In some function CFStringRef str = (__bridge CFStringRef)[NSString stringWithFormat:@"%g", 2.5]; NSLog(@"%ld", CFGetRetainCount(str)); TestClass *obj = [[TestClass alloc] init]; obj.str = str; NSLog(@"%ld", CFGetRetainCount(str)); 打印“1”两次。 现在奇怪的是(虽然我不知道这一点),我认为它正常工作之前,我更新到iOS 5.1和Xcode 4.3.1(从iOS 5和Xcode 4.2),并从gdb切换到lldb。 没有升级(或知道如何改回编译器)的人可以确认吗?