无法初始化类型' BOOL' - theos

时间:2014-10-01 12:47:28

标签: objective-c boolean objective-c++ theos tweak

我正在使用theos创建一个调整,将“幻灯片解锁”文本更改为自定义字符串

在我的Tweak.xm中:

%hook SBLockScreenView

- (void)setCustomSlideToUnlockText:(id)unlockText { 

NSString *settingsPath = @"/var/mobile/Library/Preferences/com.motion.tweak~prefs.plist";
NSMutableDictionary *prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:settingsPath];
NSString *text = [prefs objectForKey:@"text"];

BOOL enabled = [prefs objectForKey:@"enabled"];

if([text isEqualToString:@""] || text == nil || ![enabled]) {
    %orig(unlockText);

}

else if ([enabled]) {

    unlockText = text;
    %orig(unlockText);
}

}

%end

当我尝试make package时,我返回时出错:

error: cannot initialize a variable of type 'BOOL' (aka 'signed char')
      with an rvalue of type 'id'
BOOL enabled = [prefs objectForKey:@"enabled"];
     ^         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

1 个答案:

答案 0 :(得分:0)

第一期:

正如@Tim Edwards所说: 从字典返回的布尔对象作为NSNumber返回(为0或1)。因此要求NSNumber的boolean值为:

[ [prefs objectForKey:@"enabled"] booleanValue]

就下一个错误代码而言,是因为您使用if语句进行错误检查:

if([text isEqualToString:@""] || text == nil || ![enabled]) {
else if ([enabled]) {

你不需要在括号[]中加入布尔值,最好只检查你的NSString的长度,而不是对它进行2次检查,所以将两个语句都改为:

// I am accustomed with making it <= 0 even though it's impossible. 
if(!enabled || text.length <= 0) {

else if (enabled) {