我正在将我的项目转换为使用ARC.我在NSColor上有一个类别,它有一个返回自动释放的CGColor表示的方法:
@implementation NSColor (MyCategory)
- (CGColorRef)CGColor
{
NSColor *colorRGB = [self colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
CGFloat components[4];
[colorRGB getRed:&components[0]
green:&components[1]
blue:&components[2]
alpha:&components[3]];
CGColorSpaceRef space = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
CGColorRef theColor = CGColorCreate(space, components);
CGColorSpaceRelease(space);
return (CGColorRef)[(id)theColor autorelease];
}
@end
Run Code Online (Sandbox Code Playgroud)
使用ARC执行此操作的正确方法是什么?我不想返回保留的CGColor.
XCode中的ARC转换器建议使用
return (CGColorRef)[(__bridge id)theColor autorelease];
Run Code Online (Sandbox Code Playgroud)
但这会导致以下错误消息:
[rewriter]将'autorelease'消息的结果转换为'CGColorRef'是不安全的; __bridge强制转换可能会导致指向被破坏对象的指针,并且__bridge_retained可能会泄漏对象
xcode cocoa objective-c core-foundation automatic-ref-counting
我需要从CFDictionary
实例中读取和写入一些数据(以读取和更新照片中的EXIF数据).对于我的生活,我无法弄清楚如何在Swift 3中执行此操作.我想要的呼叫的签名是:
func CFDictionaryGetValue(CFDictionary!, UnsafeRawPointer!)
Run Code Online (Sandbox Code Playgroud)
我怎么把我的键(一个字符串)转换成一个UnsafeRawPointer
所以我可以将它传递给这个调用?
我想迭代CFDictionary(CFPropertyList)并获取特定级别的所有值.
这将是我的字典/属性列表:
root
A
foo
0
bar
0
B
foo
10
bar
100
C
foo
20
bar
500
Run Code Online (Sandbox Code Playgroud)
使用ObjC看起来像这样:
//dict is loaded with the dictionary below "root"
NSDictionary *dict = [...];
NSEnumerator *enumerator = [dict keyEnumerator];
NSString *key;
while (key = [enumerator nextObject])
{
NSLog(key);
};
它会打印出控制台的键列表,如下所示:
A B C
在CoreFoundation级别使用C/C++时,如何实现这一目标?
在Mac OS X中,每个显示都会CGDirectDisplayID
分配一个唯一的编号.您可以使用CGGetActiveDisplayList(
)或[NSScreen screens]
访问它们等.根据Apple的文档:
显示ID可以在进程和系统重新引导之间持续存在,并且只要某些显示参数不变,通常保持不变.
在2010年年中的MacBook Pro上,Apple开始使用自动切换Intel/nVidia显卡.笔记本电脑有两个GPU,一个低功耗的英特尔和一个高性能的nVidia.以前的双GPU笔记本电脑(2009型号)没有自动GPU切换,需要用户进行设置更改,注销,然后再次登录以进行GPU切换.即使是较旧的系统也只有一个GPU.
2010年中期的模型存在一个问题,当显示器从一个GPU切换到另一个GPU时,CGDirectDisplayID不会保持不变.例如:
我的问题是,如果由于GPU更改而改变时,如何将旧显示ID与新显示ID匹配?
想到:
我注意到显示ID只改变了2,但我没有足够的测试Mac可用来确定这是否适用于所有新MacBook Pro,或者只是我的.无论如何,如果"仅检查彼此+/- 2的显示ID",那就是一种kludge.
尝试:
CGDisplayRegisterReconfigurationCallback()
,当显示器要改变时通知前后,没有匹配的逻辑.在这个注册的方法中放置这样的东西是行不通的:
// Run before display settings change:
CGDirectDisplayID directDisplayID = ...;
io_service_t servicePort = CGDisplayIOServicePort(directDisplayID);
CFDictionaryRef oldInfoDict = IODisplayCreateInfoDictionary(servicePort, kIODisplayMatchingInfo);
// ...display settings change...
// Run after display settings change:
CGDirectDisplayID directDisplayID = ...;
io_service_t servicePort …
Run Code Online (Sandbox Code Playgroud) 我得到一个NSCFDictionary返回给我,我无法弄清楚如何使用它.我知道它的类型是NSCFDictionary,因为我打印了类,它出现了__NCSFDictionary.我无法弄清楚如何用它做任何事情.
我现在只想抓住它,但甚至无法让它工作:
NSDictionary *dict = [[NSURLCredentialStorage sharedCredentialStorage] allCredentials];
for(NSURLProtectionSpace key in [dict keyEnumerator])
{
NSCFDictionary *value = [dict objectForKey:key];
}
Run Code Online (Sandbox Code Playgroud)
allCredentials的类引用说它应该返回一个字典,其值也是字典.我的任务说明不起作用.我需要某种演员吗?
尝试打印出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]);
Run Code Online (Sandbox Code Playgroud)
但他们都打印出奇怪的东西,比如hello ÔÅÓñüÔÅ®Óñü®ÓüÅ®ÓñüÔ®ÓüÔÅ®world
...我认为"char数组"和"字符串"相同,而且与"指向字符的指针"相同?API规范说它是一个"Unicode字符的C数组"(通过Unicode,它是UTF8吗?如果是,那么它应该与ASCII兼容)...如何使它工作以及为什么这三种方式赢了干嘛?
我正在尝试在swift中进行简单的DNS查找.到目前为止,这是我的代码:
let hostRef = CFHostCreateWithName(kCFAllocatorDefault, "google.com").takeRetainedValue()
var resolved = CFHostStartInfoResolution(hostRef, CFHostInfoType.Addresses, nil)
let addresses = CFHostGetAddressing(hostRef, &resolved).takeRetainedValue() as NSArray
Run Code Online (Sandbox Code Playgroud)
此时,"地址"NSArray中的每个元素都是一个包装sockaddr结构的CFDataRef对象.
由于CFDataRef可以免费桥接到NSData,我可以像这样循环遍历它们:
for address: AnyObject in addresses {
println(address) // address is of type NSData.
}
Run Code Online (Sandbox Code Playgroud)
到目前为止这么好(我想).当我在单元测试中运行时,这会打印出有效的数据.这是我陷入困境的地方.对于我的生活,我无法弄清楚如何将NSData对象中的字节转换为sockaddr结构.
如何将address.bytes(类型为COpaquePointer?)转换为ac结构?任何帮助赞赏.我正试图弄清楚我的头撞墙.
我刚开始使用swift和cocoa.我正在尝试创建一个执行图像处理的基本应用程序.
我已经获得了图像的所有信息:
let imageRef:CGImageSourceRef = CGImageSourceCreateWithURL(url, nil).takeUnretainedValue()
let imageDict:CFDictionaryRef = CGImageSourceCopyPropertiesAtIndex(imageRef, 0, nil).takeUnretainedValue()
Run Code Online (Sandbox Code Playgroud)
该词典包含以下信息:
{
ColorModel = Gray;
DPIHeight = 300;
DPIWidth = 300;
Depth = 1;
Orientation = 1;
PixelHeight = 4167;
PixelWidth = 4167;
"{Exif}" = {
ColorSpace = 65535;
DateTimeDigitized = "2014:07:09 20:25:49";
PixelXDimension = 4167;
PixelYDimension = 4167;
};
"{TIFF}" = {
Compression = 1;
DateTime = "2014:07:09 20:25:49";
Orientation = 1;
PhotometricInterpretation = 0;
ResolutionUnit = 2;
Software = "Adobe Photoshop CS6 (Macintosh)";
XResolution = …
Run Code Online (Sandbox Code Playgroud) 我有一个ivar,它在一个对象的init中被分配:
attString = [[NSMutableAttributedString alloc] init];
Run Code Online (Sandbox Code Playgroud)
在循环中,我想清除attString的内容并重新使用它.我该怎么做呢?
谢谢!
iphone objective-c mutable core-foundation nsattributedstring
我是LLVM和Objective-C自动引用计数的新手,并且有一个关于从Objective-C函数返回CGImageRefs的问题.在手动引用计数的时代,可以简单地将CGImageRef转换为id
自动释放它,然后返回原始的CGImageRef.使用ARC,我知道您可以指示ARC系统自动释放并返回您的可保留对象,但我没有看到为CFTypeRefs执行此操作的方法.
以下是我禁用ARC的方法:
- (CGImageRef)image {
CGImageRef myImage;
id myImageID = (id)myImage;
[myImageID autorelease];
return myImage;
}
Run Code Online (Sandbox Code Playgroud)
所以,我想基本上创建一个方法,使用ARC,返回一个不属于调用者的CGImageRef.如果有更好的方法做同样的事情,我对所有想法都持开放态度.我知道UIImage
这与CGImage
财产有关.
编辑:虽然为特定文件禁用ARC是一种有效的方法,但我更喜欢在整个代码中使用纯ARC.这在与其他人共享特定文件的特定代码时非常方便,因为他们不需要更改任何给定文件的构建设置.为了利用ARC系统自动释放CFTypeRef,您可以这样做:
__attribute__((ns_returns_autoreleased))
id CGImageReturnAutoreleased (CGImageRef original) {
// CGImageRetain(original);
return (__bridge id)original;
}
Run Code Online (Sandbox Code Playgroud)
然后只需return (__bridge CGImageRef)CGImageReturnAutoreleased(myImage)
返回一个自动释放的图像.
core-graphics objective-c core-foundation ios automatic-ref-counting
core-foundation ×10
objective-c ×6
ios ×4
swift ×3
cocoa ×2
macos ×2
enumeration ×1
iokit ×1
iphone ×1
key-value ×1
multi-gpu ×1
mutable ×1
nsdictionary ×1
nsstring ×1
swift3 ×1
xcode ×1