gpt4 book ai didi

objective-c - 使用 stringWithContentsOfFile 打开通知中心 plist 时出现文本编码错误 :encoding:error:

转载 作者:行者123 更新时间:2023-12-03 17:19:56 32 4
gpt4 key购买 nike

我想编写一个函数来查找 Mac 上的“请勿打扰”是否打开。

所以我写了这个函数:

-(BOOL)dndIsOn{
NSString* path = [[NSString stringWithFormat:@"~/Library/Preferences/ByHost/com.apple.notificationcenterui.%@.plist",[self getSystemUUID]] stringByExpandingTildeInPath];
NSError *error = nil;
NSString *txtFileContents = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];

NSLog(@"string:%@ error:%@",txtFileContents,error);
if ([txtFileContents rangeOfString:@"<fals"].location == NSNotFound) {
return false;
}else{
return true;
}
}

但是 txtFileContents 字符串错误返回:

.... .plist” couldn’t be opened using text encoding Unicode (UTF-8)."

为什么会发生这种情况?

最佳答案

您可以使用dictionaryWithContentsOfFile:读取顶层是字典的.plist。如果顶层是数组,则必须使用 arrayWithContentsOfURL: 。这将自动解析该文件。

在你的例子中,顶层是一本字典。关键是doNotDisturb

您想要返回的值:

BOOL doNotDisturbSet = [[NSDictionary dictionaryWithContentsOfFile:path][@"doNotDisturb"]] boolValue];

关于objective-c - 使用 stringWithContentsOfFile 打开通知中心 plist 时出现文本编码错误 :encoding:error:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33869323/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com