2011-07-29 9 views
1

我使用的是第三方庫在iOS項目,我的工作,我到一個警告,留在這個項目上,即在這行代碼多字符的字符序列

[NSNumber numberWithUnsignedLongLong:'oaut'] 

而且警告

Multi-character character constant 

我吸在C,所以我不知道如何解決這個問題,但我敢肯定的解決方法是比較容易的。幫幫我?

編輯:更多的上下文。

@implementation MPOAuthCredentialConcreteStore (KeychainAdditions) 

- (void)addToKeychainUsingName:(NSString *)inName andValue:(NSString *)inValue { 
    NSString *serverName = [self.baseURL host]; 
    NSString *securityDomain = [self.authenticationURL host]; 
// NSString *itemID = [NSString stringWithFormat:@"%@.oauth.%@", [[NSBundle mainBundle] bundleIdentifier], inName]; 
    NSDictionary *searchDictionary = nil; 
    NSDictionary *keychainItemAttributeDictionary = [NSDictionary dictionaryWithObjectsAndKeys: (id)kSecClassInternetPassword, kSecClass, 
                           securityDomain, kSecAttrSecurityDomain, 
                           serverName, kSecAttrServer, 
                           inName, kSecAttrAccount, 
                           kSecAttrAuthenticationTypeDefault, kSecAttrAuthenticationType, 
                           [NSNumber numberWithUnsignedLongLong:"oaut"], kSecAttrType, 
                           [inValue dataUsingEncoding:NSUTF8StringEncoding], kSecValueData, 
                nil]; 


    if ([self findValueFromKeychainUsingName:inName returningItem:&searchDictionary]) { 
     NSMutableDictionary *updateDictionary = [keychainItemAttributeDictionary mutableCopy]; 
     [updateDictionary removeObjectForKey:(id)kSecClass]; 

     SecItemUpdate((CFDictionaryRef)keychainItemAttributeDictionary, (CFDictionaryRef)updateDictionary); 
     [updateDictionary release]; 
    } else { 
     OSStatus success = SecItemAdd((CFDictionaryRef)keychainItemAttributeDictionary, NULL); 

     if (success == errSecNotAvailable) { 
      [NSException raise:@"Keychain Not Available" format:@"Keychain Access Not Currently Available"]; 
     } else if (success == errSecDuplicateItem) { 
      [NSException raise:@"Keychain duplicate item exception" format:@"Item already exists for %@", keychainItemAttributeDictionary]; 
     } 
    } 
} 

編輯2:他們正試圖通過創造來滿足這個要求是NSNumber的:

@constant kSecAttrType Specifies a dictionary key whose value is the item's 
     type attribute. You use this key to set or get a value of type 
     CFNumberRef that represents the item's type. This number is the 
     unsigned integer representation of a four-character code (e.g., 
     'aTyp'). 
+1

看起來像一個錯字,因爲'oaut'絕對不是一個無符號長整數。那裏應該有什麼號碼? – MechEthan

+0

yAak:我認爲它應該有它,這是瘋狂的一部分。用更多的上下文更新後。 – refulgentis

+2

考慮到這一點,我記得多字符字符常量在C中是可能的(最多四個字符,IIRC)。也許'oaut'真的是一個這樣的角色,被用作int並被提升爲無符號long long。 'oaut'是代碼中使用的「oauth」的第一個字符。顯然CodeWarrior接受了這樣的代碼。 –

回答

1

在C和對象 - 單引號'僅用於單字符常量。您需要使用雙引號:"

像這樣:

[NSNumber numberWithUnsignedLongLong:"oaut"] 

覆蓋警告,但這裏也有一個語義問題。儘管可以將單個字符常量(如'o')視爲整數(並且可以將其提升爲unsigned long long),但是「字符串」(char *char [])不能,這意味着您不能使用"oaut"作爲參數到numberWithUnsignedLongLong:

更新:

我想四字符代碼應該被視爲一個整數,即,每個char的8位到位,就好像它們一起是一個32位int

char code[] = "oaut"; 
uint32_t code_as_int = code[0] | (code[1] << 8) | (code[2] << 16) | (code[3] << 24); 

[NSNumber numberWithUnsignedLongLong:code_as_int] 

儘管我不確定這裏會出現哪種字節順序,也不知道這是爲什麼要調用unsigned long long,除非爲了確定是否有足夠的位。

現在我認爲Rudy的評論是正確的 - 一些編譯器爲了這個目的允許多字符常量(這是「實現定義」的行爲)。

+0

hrm,應該先嚐試一下,明顯的答案。但是現在我已經發送了'char [5]'到long long類型的參數,這是真的。多麼奇怪的一段代碼。 – refulgentis

+0

看到你的編輯。不得不想知道他們在這裏試圖做什麼。 – refulgentis

+0

也許'oaut'是某個地方的'enum'的符號,而且這些引號根本不應該存在?或者它是一個風格很差的'#define'd常量? –

1

'oaut'(單引號)是一個字符,因此編譯器試圖將其解釋爲多字節字符,但無法理解它。這解釋了錯誤信息。

我想,如果你給一個適當的字符串,如@「oaut」,你會得到另一個錯誤信息,因爲numberWithUnsignedLongLong:預計的unsigned long long,而不是字符串或字符。你是否想通過名稱爲「oaut」的變量?如果是,請使用

[NSNumber numberWithUnsignedLongLong: oaut]; 

如果不是,請解釋「oaut」是什麼。

編輯

'oaut'實際上可能是原始值。 C中有/是多字符字符常量然後使用(4字節)char作爲int並提升爲unsigned long long。這必須是舊代碼。 CodeWarrior似乎接受了這樣的代碼。

假設真的是一個多字符字符常量,'oaut'看起來像一個「幻數」,並且這個值被選中,因爲它是"oauth"的開頭。我想它應該是價值0x6F617574或0x7475616F。