2014-07-02 35 views
0

我有一個的UITextField「用戶id」有即4578.的UITextField到iBeacon顯示主要的Xcode

我想將其轉換成一個比較的格式中有一個四位數字,所以我可以在iBeacon.major使用值。

我已經嘗試了很多方法我最後一次是:

long long myLongLong   = [userId.text longLongValue]; 
    NSNumber *anumber  = [NSNumber numberWithLongLong:myLongLong]; 

NSUUID * uid2 = [[NSUUID alloc] initWithUUIDString:@"INTT1345-2C29-40E0-8E1E-1D7A9E5D0964"]; 
    self.beaconRegion2 = [[CLBeaconRegion alloc] initWithProximityUUID:uid2 
                   major:anumber 
                  identifier:@"IDENTIFIER"]; 

,但他們都沒有工作,我不斷收到以下錯誤。

incompatable pointer to interger conversion sending 'NSNumber *_strong' to parameter of type 'CLBeaconMajorValue' (aka'unsigned short') 

有人可以幫助我走出這個

回答

0

試試這個:

NSNumberFormatter * formatter = [[NSNumberFormatter alloc] init]; 
userIdInt = [formatter numberFromString:self.major.text].intValue; 

ARC之前,你也可以在文本字段轉換爲整數這樣的:

int userIdInt = [userId.text intValue]; 

在將其放入主要價值之前,您應該先做一些驗證。確保它不是> 65535或< 0,因爲這些值是不允許的。就像這樣:

if (major == nil || major.intValue < 0 || major.intValue > 65535) { 
    NSLog(@"major is NOT a legal value: %@", major); 
} 
else { 
    NSLog(@"major is a legal value: %@", major); 
} 
+0

這仍然給我的錯誤,我想有相同的數字,是在textFiels是一個在主要價值 – user1114881

+0

看到我的更新答案 – davidgyoung