我有一个返回CFTypeRef的函数.我不知道它究竟是什么.我该如何确定?例如,它可能是CFStringRef.
这样做的正确方法是什么?该NSTimer文件说,这样的:
特别注意事项
您必须从安装了计时器的线程发送此消息.如果从另一个线程发送此消息,则可能无法从其运行循环中删除与计时器关联的输入源,这可能会阻止线程正常退出.
既然GCD不能保证串行队列总是在同一个线程上运行块,那么确保NSTimer在同一个线程上调度和失效的正确方法是什么?
编辑:
根据以下答案的建议,我创建了MSWeakTimer(https://github.com/mindsnacks/MSWeakTimer),这是一个使用的自定义计时器实现GCD,可以在任何队列中使用.
objective-c core-foundation nstimer grand-central-dispatch ios
我确定我的缓冲区属性出了问题,但是我不清楚它是什么 - 它没有很好地记录应该去那里的东西,所以我猜是基于CVPixelBufferPoolCreate- 而Core Foundation对我来说几乎是一本封闭的书.
// "width" and "height" are const ints
CFNumberRef cfWidth = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &width);
CFNumberRef cfHeight = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &height);
CFStringRef keys[] = {
kCVPixelBufferWidthKey,
kCVPixelBufferHeightKey,
kCVPixelBufferCGImageCompatibilityKey
};
CFTypeRef values[] = {
cfWidth,
cfHeight,
kCFBooleanTrue
};
int numValues = sizeof(keys) / sizeof(keys[0]);
CFDictionaryRef bufferAttributes = CFDictionaryCreate(kCFAllocatorDefault,
(const void **)&keys,
(const void **)&values,
numValues,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks
);
AVAssetWriterInputPixelBufferAdaptor *adaptor = [[AVAssetWriterInputPixelBufferAdaptor
assetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerInput
sourcePixelBufferAttributes:(NSDictionary*)bufferAttributes] retain];
CVPixelBufferPoolRef bufferPool = adaptor.pixelBufferPool;
NSParameterAssert(bufferPool != NULL); // fails
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用IOHIDManager获取修饰键事件,因为Cocoa flagsChanged事件缺乏(难以区分按下/释放,左/右如果两者都关闭等)这里是我创建管理器并注册回调的代码.
IOHIDManagerRef hidManager = IOHIDManagerCreate(kCFAllocatorDefault,
kIOHIDOptionsTypeNone);
if (CFGetTypeID(hidManager) != IOHIDManagerGetTypeID())
return 1;
CFMutableDictionaryRef capsLock =
myCreateDeviceMatchingDictionary(0x07, 0x39);
CFMutableDictionaryRef lctrl =
myCreateDeviceMatchingDictionary(0x07, 0xE0);
CFMutableDictionaryRef lshift =
myCreateDeviceMatchingDictionary(0x07, 0xE1);
CFMutableDictionaryRef lalt =
myCreateDeviceMatchingDictionary(0x07, 0xE2);
CFMutableDictionaryRef lsuper =
myCreateDeviceMatchingDictionary(0x07, 0xE3);
CFMutableDictionaryRef rctrl =
myCreateDeviceMatchingDictionary(0x07, 0xE4);
CFMutableDictionaryRef rshift =
myCreateDeviceMatchingDictionary(0x07, 0xE5);
CFMutableDictionaryRef ralt =
myCreateDeviceMatchingDictionary(0x07, 0xE6);
CFMutableDictionaryRef rsuper =
myCreateDeviceMatchingDictionary(0x07, 0xE7);
CFMutableDictionaryRef matchesList[] = {
capsLock,
lctrl,
lshift,
lalt,
lsuper,
rctrl,
rshift,
ralt,
rsuper
};
CFArrayRef matches = CFArrayCreate(kCFAllocatorDefault,
(const void **)matchesList, 9, …Run Code Online (Sandbox Code Playgroud) O'Reilly的编程iOS 5说(可能更宽松),"NSString是一个CFString".但至少NSString *会指出一些内存块isa,而CFStringRef不是可能不是吗?
刚刚更新到Xcode Beta 4,并注意到以下编译器错误,我的代码如下:
var path = CGPathCreateMutable()
...
CGPathRelease(path)
Run Code Online (Sandbox Code Playgroud)
'CGPathRelease'不可用:Core Foundation对象自动进行内存管理
所以,我只是删除我的发布电话,一切都应该没问题?还是有更多我想念的东西?ARC有什么特殊情况需要注意吗?
memory-management core-foundation ios automatic-ref-counting swift
我第一次能够在Xcode 7中编译我的应用程序(在测试版4和5中失败).所以,我猜这是个好进展.
但是,当我在iPhone 6,iOS 8.4.1上加载我的应用程序时,它在调试器中崩溃并显示以下消息:
dyld:未找到符号:_NSArray0 参考自:/private/var/mobile/Containers/Bundle/Application/0294DF62-AE80-485D-BB11-8C3A5D39777D/Boxtiq.app/Boxtiq预期在:/ System/Library/Frameworks/CoreFoundation. /private/var/mobile/Containers/Bundle/Application/0294DF62-AE80-485D-BB11-8C3A5D39777D/Boxtiq.app/Boxtiq中的框架/ CoreFoundation
这与链接的库的顺序有关吗?期待一些建议.
谢谢!
我正在尝试在使用CoreFoundation和Foundation的Linux上编译一些代码,但Linux并没有像macOS和iOS那样实现桥接.
Objective-C和Swift工作之间的桥梁:
import Foundation
import CoreFoundation
import Glibc
func wantsNSString(_ string: NSString) {
print(string)
}
let string = "Hello, world!"
wantsNSString(string._bridgeToObjectiveC())
Run Code Online (Sandbox Code Playgroud)
但我无法弄清楚如何桥接到CoreFoundation.我不能只是传递NSString给一个想要一个CFString:
import Foundation
import CoreFoundation
import Glibc
func wantsCFString(_ string: CFString) {
print(string)
}
let string = "Hello, world!"
wantsCFString(string._bridgeToObjectiveC()) //error: cannot convert value of type 'String._ObjectType' (aka 'NSString') to expected argument type 'CFString'
Run Code Online (Sandbox Code Playgroud)
我不能像在macOS上那样投射它:
import Foundation
import CoreFoundation
import Glibc
func wantsCFString(_ string: CFString) {
print(string)
}
let string = "Hello, world!"
wantsCFString(string._bridgeToObjectiveC() as …Run Code Online (Sandbox Code Playgroud) 我一直想知道为什么Apple在Core Foundation中使用了类型为指针类型的数据类型,而在Cocoa中却没有.
作为一个例子,你会引用一个UIColor对象,就像UIColor *对CGColor对象的引用一样CGColorRef?还是NSURL *和CFURLRef?为什么不总是使用CGColor *和CFURL *?或者相反,为什么没有UIColorRef或NSURLRef类型,因为你从来没有访问UIColor或NSURL直接访问?
或者例如,它为什么id而不是id *,因为它实际上是一个指针,实际上可以强制转换为void *?
具体来说,是否有一些原因让Apple习惯在旧框架中执行此操作,但是在Cocoa中停止了这样做?这只是风格问题吗?
我发现它是开源的,我正在查看一些CFArray代码,我发现了一些奇怪的代码.这些"空" if (0)条件有什么意义?是否有一些疯狂的好处,或者这只是遗留下来的东西?此代码位于GitHub的CFArray.c的第957行.
if (0) {
}
else if (NULL == array->_store) {
if (0) {
}
else if (0 <= futureCnt) {
// blah blah
}
}
Run Code Online (Sandbox Code Playgroud) core-foundation ×10
objective-c ×5
ios ×4
swift ×2
beta ×1
cocoa ×1
core-video ×1
foundation ×1
if-statement ×1
iokit ×1
keyboard ×1
linux ×1
macos ×1
macos-carbon ×1
nstimer ×1
xcode ×1
xcode7-beta6 ×1