标签: core-foundation

如何使用C找到应用程序包(NSBundle)中文件的路径?

是否有用于查找应用程序包中文件路径的C API?

我知道这可以在Objective-C中使用以下语法完成.

NSString *path = [[NSBundle mainBundle] pathForResource:@"MyImage" ofType:@"bmp"];
Run Code Online (Sandbox Code Playgroud)

我是否可以从C或C++代码调用相应的函数?

c macos core-foundation nsbundle

7
推荐指数
2
解决办法
6336
查看次数

使用IOKit获取USB设备的BSD名称以写入设备?

我试图在MacOS下使用IOKit确定虚拟串口的BSD名称.

我有一个看起来像虚拟串口的USB CDC设备,我想得到BSD设备路径,这样我就可以做一个fopen("/ dev/tty.usbmodem123").我有一个程序,它接受VID和PID并等待插入设备,然后我想使用BSD名称写入设备.设备在每个系统上的安装方式不同,我试图将其用作教学工具,因此我需要在写入设备之前先搜索设备,而无需手动检查/dev/tty.*设备的安装位置.

我有3个问题.

首先,可以使用CFSTR(kIOBSDNameKey)?获取虚拟串口的BSD名称吗? IORegistryEntrySearchCFProperty()FindProp()始终返回"null".有谁知道非块设备是否可以返回BSD名称?我目前正在这样做:

bsdName = IORegistryEntrySearchCFProperty(p_usb_ref, kIOServicePlane, CFSTR(kIOBSDNameKey), kCFAllocatorDefault, kIORegistryIterateRecursively );

其次,我已经能够得到服务平面名称:IOService:/ AppleACPIPlatformExpert/PCI0 @ 0/AppleACPIPCI/OHC1 @ 4/AppleUSBOHCI /简介电子@ 4100000,这对应于一个挂载点:/dev/tty.usbmodem411有谁知道如何将服务平面名称转换为开发树名称?

第三,我是不是太复杂了?我已经知道设备io处理了,有没有办法用它来向设备写入数据?我只需要发送一些ASCII字节来闪存一些LED.

任何建议将不胜感激.

EDIT:

花了一些时间看这个之后,我发现我的问题是我在加载CDC驱动程序之前查询了BSD名称.我目前正在获取BSD名称,然后整理出VID和PID.

解决上述问题的代码是:

matchingDictionary = IOServiceMatching(kIOSerialBSDServiceValue);
CFDictionarySetValue(matchingDictionary, CFSTR(kIOSerialBSDTypeKey), CFSTR(kIOSerialBSDModemType));
kernResult = IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDictionary, &iter);
Run Code Online (Sandbox Code Playgroud)

然后迭代iter查找具有正确ID的设备.

macos iokit core-foundation

7
推荐指数
1
解决办法
2076
查看次数

这些if(0)条件有什么意义?

我发现它是开源的,我正在查看一些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)

if-statement objective-c core-foundation

7
推荐指数
1
解决办法
1015
查看次数

如何在0.3秒后完成给定次数的动作?

let expecation = expectationWithDescription("do tasks")
for i in 0...40 {

    let afterTiming = 0.3 * Double(i)
    let startTime = CFAbsoluteTimeGetCurrent()

    let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(afterTiming * Double(NSEC_PER_SEC)))

    dispatch_after(delayTime, dispatch_get_main_queue()) {
        let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime
        print("\(afterTiming) - \(timeElapsed) : \(i)")
    }
}

waitForExpectationWithTimeout(14)
Run Code Online (Sandbox Code Playgroud)

在30执行它几乎一秒钟之后,控制台开始表现奇怪,同时显示两条和两条打印线

9.0 - 9.88806998729706 : 30
9.3 - 9.88832598924637 : 31
Run Code Online (Sandbox Code Playgroud)

有没有办法XCTest让我们更接近"在正确的时间"实际执行请求?就像在9.88秒后没有完成9秒后应该完成的请求一样.

core-foundation grand-central-dispatch swift

7
推荐指数
1
解决办法
324
查看次数

无法识别的选择器发送到实例_UIAlertControllerAlertPresentationController

errors我的iOS应用程序中有几个实时场景.我无法理解这个Core Foundation影响我的应用程序中多个用户的特定错误.我的任何项目文件中的流程都没有中断,我不确定导致此崩溃的原因.

附加崩溃日志.任何建议或帮助都会给你很大的帮助.

CoreFoundation _CF_forwarding_prep_0

致命异常:NSInvalidArgumentException - [_ UIAlertControllerAlertPresentationController adaptivePresentationController]:无法识别的选择器发送到实例0x133e29870

Fatal Exception: NSInvalidArgumentException
0  CoreFoundation                 0x18ff391b8 __exceptionPreprocess
1  libobjc.A.dylib                0x18e97055c objc_exception_throw
2  CoreFoundation                 0x18ff40268 __methodDescriptionForSelector
3  CoreFoundation                 0x18ff3d270 ___forwarding___
4  CoreFoundation                 0x18fe3680c _CF_forwarding_prep_0
5  UIKit                          0x19689708c -[UISearchController _searchPresentationController]
6  UIKit                          0x19648f8cc -[_UISearchControllerTransplantSearchBarAnimator animateTransition:]
7  UIKit                          0x19613d464 __56-[UIPresentationController runTransitionForCurrentState]_block_invoke
8  UIKit                          0x19607ffdc _runAfterCACommitDeferredBlocks
9  UIKit                          0x196071d50 _cleanUpAfterCAFlushAndRunDeferredBlocks
10 UIKit                          0x195de10b4 _afterCACommitHandler
11 CoreFoundation                 0x18fee60c0 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__
12 CoreFoundation                 0x18fee3cf0 __CFRunLoopDoObservers
13 CoreFoundation                 0x18fee4180 __CFRunLoopRun
14 CoreFoundation                 0x18fe122b8 …
Run Code Online (Sandbox Code Playgroud)

selector core-foundation ios invalidargumentexception swift

7
推荐指数
1
解决办法
1355
查看次数

使用ARC不允许使用指向'CFReadStreamRef*'的Objective-C指针的间接指针

我想CFStreamCreatePairWithSocketToHostNSInput- 和OutputStreams.我有两个伊娃NSInputStream *_inputStreamNSOutputStream *_outputStream.

以下给出了两条错误消息:

CFStreamCreatePairWithSocketToHost(NULL,
  (__bridge_retained CFStringRef)self.hostname, self.port,
  (CFReadStreamRef *)&_inputStream, (CFWriteStreamRef *)&_outputStream);
Run Code Online (Sandbox Code Playgroud)

错误:一个间接指针的浇铸到一个Objective-C指针" CFReadStreamRef *"(又名" struct __CFReadStream **')是不允许与ARC
错误:一个间接指针的浇铸到一个Objective-C指针’ CFWriteStreamRef *"(又名" struct __CFWriteStream **")是不允许的同ARC

我该如何解决这个问题?我试过使用__bridge但是我得到了类似的错误消息.

objective-c core-foundation multiple-indirection foundation automatic-ref-counting

6
推荐指数
1
解决办法
2152
查看次数

使用CFStringRef和CFRelease进行适当的内存管理

考虑这个简单的方法; 它是NSString上的一个类别.

- (NSString *)stringByUrlEncoding
{
    CFStringRef newString = CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)self, NULL, (CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ", kCFStringEncodingUTF8);
    NSString *returnString = (__bridge NSString *)newString;
    CFRelease(newString);
    return returnString;
}
Run Code Online (Sandbox Code Playgroud)

它的任务是把=%3D等为URL编码,但是这不是有关这个问题.

关于内存管理的一些问题:

  1. 之后CFRelease(newString);被调用时,我仍然可以po newString在调试器中看到的字符串.这是否意味着它没有得到正确的解除分配?

  2. 当我将已铸造的CFStringRefs传递给CFURLCreateStringByAddingPercentEscapes的参数时((CFStringRef)self例如),我相信我不需要将它们存储到变量中,然后由于"免费桥接"而CFRelease它们.它是否正确?

  3. 如果它是CGImageRef而不是CFStringRef,#2的答案是否会改变?为什么CGImageRef有自己的CGImageRelease函数,但是没有CFStringRelease方法?

  4. 为了确保我的理解__bridge_transfer是正确的,这个修改后的代码是否相同?

    - (NSString *)stringByUrlEncoding
    {
        CFStringRef newString = CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)self, NULL, (CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ", kCFStringEncodingUTF8);
        NSString *returnString = (__bridge_transfer NSString *)newString;
        // CFRelease(newString);
        return returnString;
    }
    
    Run Code Online (Sandbox Code Playgroud)

objective-c core-foundation automatic-ref-counting

6
推荐指数
1
解决办法
891
查看次数

从CGWindowListCopyWindowInfo获取NSWindow*

我已经完成了列出所有窗口(按照从前到后的顺序,我认为/希望)使用,CGWindowListCopyWindowInfo但我有一个问题得到NSWindow*它,所以我可以使用orderFront:等.

看来我甚CGWindowID至都没有得到它.

这是我的代码,它是js-ctypes.

var cfarr_win = ostypes.API('CGWindowListCopyWindowInfo')(ostypes.CONST.kCGWindowListOptionAll | ostypes.CONST.kCGWindowListExcludeDesktopElements, ostypes.CONST.kCGNullWindowID);

var cnt_win = ostypes.API('CFArrayGetCount')(cfarr_win);

for (var i = 0; i < cnt_win; i++) {
    var thisWin = {};
    // trying to get NSWindow* to the window here, so i can use with orderFront: etc

    // example on how i get pid:
    var rez_pid = ostypes.API('objc_msgSend')(c_win, ostypes.HELPER.sel('objectForKey:'), myNSStrings.get('kCGWindowOwnerPID'));
    var int_pid = ostypes.API('objc_msgSend')(rez_pid, ostypes.HELPER.sel('integerValue'));
    thisWin.pid = int_pid;

    // How can I get NSWindow*

} …
Run Code Online (Sandbox Code Playgroud)

macos core-graphics objective-c core-foundation

6
推荐指数
1
解决办法
2474
查看次数

CFRunLoopObserver与嵌套CFRunLoops

我有一个Cocoa UI应用程序,需要像其他UI事件循环许可一样快地更新其主窗口,所以我通过CFRunLoopObserver实现了主更新程序功能.我还有一个应用程序模式警报框和一个上下文菜单.

所以,我的问题是,当我在屏幕上同时显示警报和上下文菜单时,更新程序会卡住,可能是由于嵌套的运行循环数量超过了它在开始时附加到它上面的数量.令人惊讶的是,我可以通过复制观察者来缓解这个问题,并且它一直在运行 - 只有一个实例,从«blocking»嵌套循环开始时从现有的两个实例中随机选择.当它结束时(选择一个菜单项,一个警报超过其超时或手动解除 - 无论如何),事情恢复正常,被阻止的观察者重新获得运行能力.

现在的问题是:有没有办法让一个观察者得到执行,无论运行循环堆栈有多深?

macos cocoa core-foundation

6
推荐指数
1
解决办法
173
查看次数

iOS 13 - 崩溃:com.apple.main-thread EXC_BREAKPOINT (CoreFoundation)

我已经开始收到大量来自Fabric已在 App Store 上发布的 iOS 应用程序的崩溃报告。崩溃仅从18 Sep以后开始报告iOS 13。这是崩溃的主线程的崩溃日志。

Crashed: com.apple.main-thread
0  CoreFoundation                 0x185180408 OUTLINED_FUNCTION_0 + 4
1  CoreFoundation                 0x18525bd68 __CFStringCollectionCopy.cold.1 + 14
2  CoreFoundation                 0x18525bd68 __CFStringCollectionCopy.cold.1 + 14
3  CoreFoundation                 0x18525bd68 __CFStringCollectionCopy.cold.1 + 14
4  CoreFoundation                 0x18525bd68 __CFStringCollectionCopy.cold.1 + 14
5  CoreFoundation                 0x18525bd68 __CFStringCollectionCopy.cold.1 + 14
6  CoreFoundation                 0x18525bd68 __CFStringCollectionCopy.cold.1 + 14
7  CoreFoundation                 0x18525bd68 __CFStringCollectionCopy.cold.1 + 14
8  CoreFoundation                 0x18525bd68 __CFStringCollectionCopy.cold.1 + 14
9  CoreFoundation                 0x18525bd68 __CFStringCollectionCopy.cold.1 + 14
10 CoreFoundation                 0x18525bd68 __CFStringCollectionCopy.cold.1 …
Run Code Online (Sandbox Code Playgroud)

crash core-foundation ios ios13

6
推荐指数
1
解决办法
4047
查看次数