【问题标题】:Custom function not returning correct value自定义函数没有返回正确的值
【发布时间】:2016-10-16 21:47:57
【问题描述】:

我有一个代码来创建我自己的函数,但它给了我一个警告:

  • 从“int”分配给“SMUserKey *”(又名“unsigned long *”)的指针转换不兼容的整数
  • 不兼容的整数到指针转换将“SMUserKey”(又名“unsigned long”)发送到“SMUserKey *”(又名“unsigned long *”)类型的参数

所以它显示警告,我想摆脱它们。

这是我的 .h 代码:

typedef NSUInteger SMUserKey;


NS_ENUM(SMUserKey) {
    SMUsername = 1,
    SMFirstName = 2,
    SMLastName = 3,
    SMFullName = 4,
    SMEmail = 5,
};

这是我的 .m 代码:​​

-(void)awakeFromNib {
    NSString *result = [SMUser getValueForKey:SMUsername];
    NSLog(@"THE RESULT: %@", result);
}

+ (NSString *)getValueForKey:(SMUserKey*)myUserKey {
    NSString *myUserKeyValue = nil;
    if (myUserKey == 1) {
        myUserKeyValue = @"Username";
    }
    if (myUserKey == 2) {
        myUserKeyValue = @"First Name";
    }
    if (myUserKey == 3) {
        myUserKeyValue = @"Last Name";
    }
    if (myUserKey == 4) {
        myUserKeyValue = @"Full Name";
    }
    if (myUserKey == 5) {
        myUserKeyValue = @"Email";
    }
    return myUserKeyValue;
}

这是显示警告的图片:

Click here to see the image

【问题讨论】:

    标签: c++ ios objective-c macos function


    【解决方案1】:

    条件检查应该是if (myUserKey == 1)而不是(myUserKey = 1),其余的修改相同。

    【讨论】:

    • 好的,谢谢。有用。但现在我仍然有警告。我错过了什么吗?
    • 这次的警告是什么?
    • 我能够修复函数的警告,但是当我调用函数 NSString *myUserKeyValue = nil; if ((int)myUserKey == 1) { myUserKeyValue = usernameValue; } else if ((int)myUserKey == 2) { myUserKeyValue = firstnameValue; } else if ((int)myUserKey == 3) { myUserKeyValue = lastnameValue; } else if ((int)myUserKey == 4) { myUserKeyValue = fullnameValue; } else if ((int)myUserKey == 5) { myUserKeyValue = emailValue; } 返回我的用户键值;
    【解决方案2】:

    只是发布另一种使用开关盒的方法,

    + (NSString *)getValueForKey:(SMUserKey*)myUserKey {
    NSString *myUserKeyValue = nil;
    
    switch (SMUserKey) {
        case 1:
        {
            myUserKeyValue = @"Username";
            break;
        }
        case 2:
        {
            myUserKeyValue = @"First Name";
            break;
        }
        case 3:
        {
            myUserKeyValue = @"Last Name";
            break;
        }
        case 4:
        {
            myUserKeyValue = @"Full Name";
            break;
        }
        case 5:
        {
            myUserKeyValue = @"Email";
            break;
        }
    
        default:
            break;
    }
    
    return myUserKeyValue;
    

    }

    【讨论】:

      【解决方案3】:

      你有枚举名称。 使用它们

      + (NSString *)getValueForKey:(SMUserKey)myUserKey {
          switch(myUserKey) {
          case SMUsername:  return @"Username";
          case SMFirstName: return @"First Name";
          case SMLastName:  return @"Last Name";
          case SMFullName:  return @"Full Name";
          case SMEmail:     return @"EMail";
          default:          return nil;
          }
      }
      

      硬编码您的幻数会导致长期维护问题。

      注意参数类型应该是SMUserKey而不是SMUserKey*

      【讨论】:

        【解决方案4】:

        应该这样做。

        -(void)awakeFromNib {
            NSString *result = [SMUser getValueForKey:SMUsername];
            NSLog(@"THE RESULT: %@", result);
        }
        
        + (NSString *)getValueForKey:(SMUserKey*)myUserKey {
            NSString *myUserKeyValue = nil;
            if (myUserKey == 1) {
                myUserKeyValue = @"Username";
            }
            if (myUserKey == 2) {
                myUserKeyValue = @"First Name";
            }
            if (myUserKey == 3) {
                myUserKeyValue = @"Last Name";
            }
            if (myUserKey == 4) {
                myUserKeyValue = @"Full Name";
            }
            if (myUserKey == 5) {
                myUserKeyValue = @"Email";
            }
            return myUserKeyValue;
        }
        

        【讨论】:

        • 不!您将 NSUInteger 类型转换为指针,然后将指针与硬编码数字进行比较。修正你的参数类型。
        猜你喜欢
        • 1970-01-01
        • 2018-09-29
        • 2017-08-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多