我已经设置了一个简单的NSURLConnection来查询http服务器.
GET /path HTTP/1.1
Host: 192.168.1.161:8282
User-Agent: NetTest1.0 CFNetwork/441.0.2 Darwin/9.6.0
Accept: */*
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Pragma: no-cache
Connection: keep-alive
Run Code Online (Sandbox Code Playgroud)
服务器使用代码401和WWW-Authenticate标头集进行响应
HTTP/1.1 401
Connection: close
Pragma: no-cache
Expires: Thu, 01 Dec 1994 16:00:00 GMT
Cache-control: no-cache
Cache-last-checked: Thu, 01 Dec 1994 16:00:00 GMT
Last-modified: Tue, 07 Apr 02009 22:55:48 CEST
Content-type: text/html; charset=iso-8859-1
WWW-Authenticate: Basic realm:
Run Code Online (Sandbox Code Playgroud)
我想这会向我的委托的连接发送一条消息:didReceiveAuthenticationChallenge:方法,但事实并非如此.
我也实施了
- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection {
return FALSE;
}
Run Code Online (Sandbox Code Playgroud)
只是为了确保它不会尝试从我的钥匙串发送缓存的凭据,而不是.
我听说做这样的事情是个坏主意.但我确信有一些经验法则可以帮助你做到这一点.
当我经常遍历NSMutableDictionary或NSMutableArray时,我需要摆脱条目.典型案例:您迭代它,并将条目与某些内容进行比较.有时结果是"不再需要",你必须删除它.但这样做会影响所有行的索引,不是吗?
那么我怎么能安全地迭代它而不会意外超出界限或跳过尚未检查过的元素?
matchingDict = IOServiceMatching(kIOUSBDeviceClassName);
numberRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vendorId);
CFDictionarySetValue(matchingDict, CFSTR(kUSBVendorID), numberRef);
CFRelease(numberRef);
numberRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &deviceProductId);
CFDictionarySetValue(matchingDict, CFSTR(kUSBProductID), numberRef);
CFRelease(numberRef);
numberRef = NULL;
kr = IOServiceAddMatchingNotification(gNotifyPort,
kIOFirstMatchNotification,
matchingDict,
DeviceAdded,
NULL,
&gAddedIter);
Run Code Online (Sandbox Code Playgroud)
为了在将USB设备添加到Mac PC时处理通知是可以的,但是当用户按下USB设备上的按钮时,我能获得信号吗?
谢谢大家!
考虑一些涉及错误处理的典型CF代码,比如说:
ABRecordRef aRecord = ABPersonCreate();
CFErrorRef anError = NULL;
ABRecordSetValue(aRecord, kABPersonFirstNameProperty, CFSTR("Joe"), &anError);
Run Code Online (Sandbox Code Playgroud)
anError这段代码后如何处理?我是否必须保留它,以确保它不会消失,然后再释放它?或者我已经是主人了,我只需要稍后发布它?
有时我的应用程序崩溃了 CFRelease(theURL);
CFURLRef theURL = CFURLCreateFromFSRef( kCFAllocatorDefault, inRef );
NSString *currentPath = [(NSURL *)theURL path];
CFRelease(theURL);
Thread 0 Crashed:
0 com.apple.CoreFoundation 0x92a53354 CFRelease + 36
Run Code Online (Sandbox Code Playgroud)
为什么?
创建规则
Core Foundation函数具有指示您拥有返回对象的名称:
在名称中嵌入"创建"的对象创建函数; 对象复制函数,名称中嵌入了"复制".如果您拥有一个对象,那么当您完成它后,您有责任放弃所有权(使用CFRelease).
如何最好输出以下代码
#include <CoreFoundation/CoreFoundation.h> // Needed for CFSTR
int main(int argc, char *argv[])
{
char *c_string = "Hello I am a C String. :-).";
CFStringRef cf_string = CFStringCreateWithCString(0, c_string, kCFStringEncodingUTF8);
// output cf_string
//
}
Run Code Online (Sandbox Code Playgroud) 我有一个CFArrayRef主要有CFDictionaryRef,但有时它会包含其他东西.如果可以的话,我想从数组中的字典中访问一个值,如果不能,我不会崩溃.这是代码:
bool result = false;
CFArrayRef devices = CFArrayCreateCopy(kCFAllocatorDefault, SDMMobileDevice->deviceList);
if (devices) {
for (uint32_t i = 0; i < CFArrayGetCount(devices); i++) {
CFDictionaryRef device = CFArrayGetValueAtIndex(devices, i);
if (device) { // *** I need to verify this is actually a dictionary or actually responds to the getObjectForKey selector! ***
CFNumberRef idNumber = CFDictionaryGetValue(device, CFSTR("DeviceID"));
if (idNumber) {
uint32_t fetched_id = 0;
CFNumberGetValue(idNumber, 0x3, &fetched_id);
if (fetched_id == device_id) {
result = true;
break;
}
} …Run Code Online (Sandbox Code Playgroud) c introspection objective-c core-foundation respondstoselector
我需要一个可以存储任何类型对象的 Swift 字典。一些值将作为CGColor参考。我在创建字典和存储CGColor参考文献方面没有问题。问题是试图安全地让他们回来。
let color = CGColor(gray: 0.5, alpha: 1)
var things = [String:Any]()
things["color"] = color
things["date"] = Date()
print(things)
Run Code Online (Sandbox Code Playgroud)
那行得通,我得到了合理的输出。后来我希望得到颜色(字典中可能存在也可能不存在。所以我很自然地尝试以下操作:
if let color = things["color"] as? CGColor {
print(color)
}
Run Code Online (Sandbox Code Playgroud)
但这会导致错误:
错误:有条件地向下转换为 CoreFoundation 类型“CGColor”将始终成功
最后我想出了:
if let val = things["color"] {
if val is CGColor {
let color = val as! CGColor
print(color)
}
}
Run Code Online (Sandbox Code Playgroud)
这在操场上没有任何警告,但在我的实际 Xcode 项目中,我在线上收到警告if val is CGColor:
'is' 测试总是正确的,因为 'CGColor' 是一个 Core Foundation 类型
这个问题有什么好的解决办法吗?
我正在处理核心图形和图层,代码需要同时适用于 iOS 和 …
有没有办法在 iPadOS 13.1 中检索已安装字体的列表?
两者都不
CTFontManagerCopyAvailablePostScriptNames
也不
[UIFont familyNames]
将获得 iPad 设置 > 常规 > 字体中显示的任何字体。有什么我想念的吗?
我正在编写一个用于同步密码的小工具。我为此使用自己的KeyChain。在保存之前,我想清除此KeyChain。但是,似乎我不明白如何使用SecItemDelete函数。
NSMutableDictionary *deleteQuery = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
kSecClassGenericPassword, kSecClass,
kSecMatchLimit, kSecMatchLimitAll, nil];
OSStatus status = SecItemDelete((__bridge CFDictionaryRef)deleteQuery);
NSLog(@"%@", SecCopyErrorMessageString(status, NULL));
Run Code Online (Sandbox Code Playgroud)
到目前为止,这就是我写的内容,但是不幸的是,我的项目(称为Root.Foo和Root.Bar)仍保留在KeyChain中。我也想知道,该函数如何知道应该搜索哪个KeyChain?我喜欢的大多数示例都是关于iOS的,默认情况下每个应用程序都有自己的KeyChain。
谢谢你的帮助 :)
core-foundation ×10
objective-c ×6
macos ×5
c ×3
cocoa ×3
ios ×2
cocoa-touch ×1
iokit ×1
iphone ×1
keychain ×1
swift ×1
usb ×1