Objective c 使用stringWithContentsOfFile:encoding打开通知中心列表时出现文本编码错误:错误:
我想写一个函数,找出Mac上是否打开了“请勿打扰” 所以我写了这个函数:Objective c 使用stringWithContentsOfFile:encoding打开通知中心列表时出现文本编码错误:错误:,objective-c,string,macos,cocoa,Objective C,String,Macos,Cocoa,我想写一个函数,找出Mac上是否打开了“请勿打扰” 所以我写了这个函数: -(BOOL)dndIsOn{ NSString* path = [[NSString stringWithFormat:@"~/Library/Preferences/ByHost/com.apple.notificationcenterui.%@.plist",[self getSystemUUID]] stringByExpandingTildeInPath]; NSError *error = n
-(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;
}
}
-(BOOL)dndIsOn{
NSString*path=[[NSString stringWithFormat:@“~/Library/Preferences/ByHost/com.apple.notificationcenterui.%@.plist”,[self-getSystemUUID]]stringByExpandingTildeInPath];
n错误*错误=nil;
NSString*txtFileContents=[NSString stringWithContentsOfFile:路径编码:NSUTF8StringEncoding错误:&错误];
NSLog(@“字符串:%@错误:%@”,txtFileContents,错误);
如果([txtFileContents rangeOfString:@“您可以使用dictionaryWithContentsOfFile:
读取.plist,其中顶层是字典。如果顶层是数组,则必须使用arrayWithContentsOfURL:
。这将自动解析文件
在您的例子中,最高层是词典。
该键为请勿打扰
要返回的值:
BOOL doNotDisturbSet = [[NSDictionary dictionaryWithContentsOfFile:path][@"doNotDisturb"]] boolValue];
您可以使用dictionaryWithContentsOfFile:
读取顶层为字典的.plist。如果顶层为数组,则必须使用arrayWithContentsOfURL:
。这将自动解析文件
在您的例子中,最高层是词典。
该键为请勿打扰
要返回的值:
BOOL doNotDisturbSet = [[NSDictionary dictionaryWithContentsOfFile:path][@"doNotDisturb"]] boolValue];
为什么要将.plist作为文本文件读取?返回[[NSDictionary Dictionary WithContentsOfFile:path][@“DonotDistribut”]]boolValue];
?太棒了!效果不错。但仍然对文本错误感到困惑!?我不知道。我用文本编辑器打开了我的字典,第一行显示:“。但由于它是一个Plist,如果您将其作为一个简单的文本文件读取,则会错过所有解析过程。cmd行上的可选项默认值为~/Library/Preferences/ByHost/com.apple.notificationcenterui.*.donotDistribut
为什么将.Plist作为文本文件读取?返回[[NSDictionary Dictionary Dictionary WithContentsofFile:path][@”donotDistribut”]boolValue];
?棒极了!很不错。但仍然对文本错误感到困惑吗?我不知道。我用文本编辑器打开了我的,第一行写着:“.但由于它是一个Plist,如果您将其作为一个简单的文本文件读取,则会错过所有解析。cmd行上的可选项defaults read~/Library/Preferences/ByHost/com.apple.notificationcenterui.*请勿打扰