core-foundation

Get peer IP address and port on OSX in objective-c from NSStream, CFStream or Socket

廉价感情. 提交于 2019-12-06 09:50:58
问题 I have written a server that listens on a specific port for incoming tcp connections. To manage the network connectivity I am using Streams (CFStream/NSStream). When a connection is esthablished I save all the information about this very connection in another instance of a dedicated class which is also set as the delegate for the streams. Now I want to get the public IP of the device that sends me a message through the already esthablished streams, in other words, I would like to store the IP

Core Foundation equivalents for NSURLRequest and NSURLConnection

随声附和 提交于 2019-12-06 08:10:58
问题 I'm aware that NSUrl is bridged to CFUrl. What are the Core Foundation equivalents for NSURLRequest and NSURLConnection so I can do something with a CFUrl object using pure C? 回答1: There are not direct equivalents to NSURLRequest and NSURLConnection in Core Foundation, but you can use the lower-level CFHTTP functions in CFNetwork. For example, CFHTTPMessageRef myRequest = CFHTTPMessageCreateRequest(kCFAllocatorDefault, requestMethod, myUrl, kCFHTTPVersion1_1); CFHTTPMessageSetBody(myRequest,

Message sent to deallocated instance with ARC using custom getter and setter

旧街凉风 提交于 2019-12-06 07:55:54
I'm trying to implement a custom getter and setter for my custom object HFObject and my app crashed with a Message sent to deallocated instance error despite using ARC. I've read every single related post, the ones that were written pre-ARC don't apply, and everything else didn't help. I have the zombie object debugger option turned on. Setting up the custom HObject Within HObject.h I have declared these four properties: @property (retain) NSString *email; //Will use custom getter/setter @property (retain) NSString *firstName; //Will use custom getter/setter @property (retain) NSDate *date; /

Convert NSData to sockaddr struct in swift

左心房为你撑大大i 提交于 2019-12-05 17:30:07
问题 I'm trying to do a simple DNS lookup in swift. So far, here is the code that I have: let hostRef = CFHostCreateWithName(kCFAllocatorDefault, "google.com").takeRetainedValue() var resolved = CFHostStartInfoResolution(hostRef, CFHostInfoType.Addresses, nil) let addresses = CFHostGetAddressing(hostRef, &resolved).takeRetainedValue() as NSArray At this point, each element in the "addresses" NSArray is a CFDataRef object wrapping a sockaddr struct. Since CFDataRef can be toll-free bridged to

How to create a CFDictionary in an OS X target?

风格不统一 提交于 2019-12-05 13:45:20
According to documentation CFDictionaryCreate is used to instantiate CFDictionary in swift . func CFDictionaryCreate(_ allocator: CFAllocator!, _ keys: UnsafeMutablePointer<UnsafePointer<Void>>, _ values: UnsafeMutablePointer<UnsafePointer<Void>>, _ numValues: CFIndex, _ keyCallBacks: UnsafePointer<CFDictionaryKeyCallBacks>, _ valueCallBacks: UnsafePointer<CFDictionaryValueCallBacks>) -> CFDictionary! How can I create the keys and values arguments? So far I've tried to use swift's String type hoping it would be automatically converted to appropriate types: import Foundation var keys : [String]

NSDate as keys for NSDictionary

依然范特西╮ 提交于 2019-12-05 12:37:46
Is it possible to add NSDate as keys and some arrays as it values in a NSDictionary? I dont have any requirement of writing the dictionary to disk - archiving or unarchiving, just as this guy needs it: NSDictionary with NSDates as keys But, why I want NSDate to be added as keys specifically is so that I can fetch all keys from the dictionary and do some computations like, fetching the dates from within a range. Whether two objects with same date value share same memory when created? Is it possible to have NSDate as key? Also, what other problems I might face with this assumption? Thanks, Raj

CFPropertyListCreateDeepCopy fails to process array / dictionary containing NSNull

旧街凉风 提交于 2019-12-05 12:28:38
For some reason this sample code works: NSArray *immutable = @[ @"a", @"b", @"c" ]; NSMutableArray *mutable = (__bridge id)CFPropertyListCreateDeepCopy(kCFAllocatorDefault, (__bridge CFArrayRef)immutable, kCFPropertyListMutableContainers); and this code produces nil as a result of the conversion: NSArray *immutable = @[ @"a", [NSNull null], @"c" ]; NSMutableArray *mutable = (__bridge id)CFPropertyListCreateDeepCopy(kCFAllocatorDefault, (__bridge CFArrayRef)immutable, kCFPropertyListMutableContainers); I tried to find any mention of NSNull not being allowed when using this function. I have a

Core Foundation objects and retain/release messages

不问归期 提交于 2019-12-05 07:00:19
问题 Let's say we have some Core Foundation objects, such as CGColorRef , that are added to an NSArray like this: CGColorRef color = ...; NSArray *array = [NSArray arrayWithObject:(id)color]; Since arrays retain their contents, color receives a retain message (not CFRetain() , right?). What happens in this case from memory management point of view? 回答1: From Core Foundation Design Concepts: Note from the example that the memory management functions and methods are also interchangeable—you can use

Unmounting Drive/volume without ejecting

丶灬走出姿态 提交于 2019-12-05 04:38:17
问题 I am want to unmount a disk WITHOUT EJECTING. To do that I tried following code { NSString *path; CFStringRef *volumeName=(__bridge CFStringRef)path; DASessionRef session = DASessionCreate(kCFAllocatorDefault); CFURLRef pathRef = CFURLCreateWithString(NULL, CFSTR("/volumes/Untitled"), NULL); DADiskRef disk = DADiskCreateFromVolumePath(kCFAllocatorDefault, session, pathRef); DADiskUnmount(disk, kDADiskUnmountOptionForce, NULL, NULL); } This code is from this question, Thanks to @zeFree Its

In Objective-C, how to print out N spaces? (using stringWithCharacters)

天涯浪子 提交于 2019-12-05 02:49:56
The following is tried to print out N number of spaces (or 12 in the example): 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]); But they all print out weird things such as hello ÔÅÓñüÔÅ®Óñü®ÓüÅ®ÓñüÔ®ÓüÔÅ®world ... I thought a "char array" is the same as a "string" and the same as a "pointer to a character"? The API spec says it is to be a "C array of