【问题标题】:cannot initialize a variable of type 'BOOL' - theos无法初始化“BOOL”类型的变量 - theos
【发布时间】:2014-11-26 07:36:14
【问题描述】:

我正在使用 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"];
     ^         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

【问题讨论】:

  • 我相信 [pref objectForKey:@"enabled"] 将返回一个 NSNumber* 类型。
  • Tim 是正确的,因此将其更改为 [[prefs objectForKey:@"enabled"] boolValue]
  • @TimEdwards @ŃikeKamstra 谢谢你们,当我尝试制作包时,我现在返回一个新错误:Tweak.xm:41:58: error: expected identifier if([text isEqualToString:@""] || text == nil || ![enabled]) { ^ Tweak.xm:46:18: error: expected identifier else if ([enabled]) { ^
  • 我把它改成答案。

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


【解决方案1】:

第一期:

正如@Tim Edwards 所说: 从字典返回的布尔对象返回为 NSNumber(0 或 1)。所以询问 NSNumber 的布尔值:

[ [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) {

【讨论】:

    猜你喜欢
    • 2017-02-23
    • 1970-01-01
    • 2019-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多