簡體   English   中英

無法識別的選擇器錯誤

[英]Unrecognized selector error

我收到此錯誤:[NSURL stringByAppendingFormat:]:無法識別的選擇器,當它到達追加時發送到實例0x5869210。 strcust是一個簡單的數字,strURI是正確的,直到追加。

 NSString *strUIR = [NSURL URLWithString:@"https://cid.hooru.mobi:36610/?";
 strURI = [strURI stringByAppendingFormat:@&Cust=IPRR%@", strCust];

任何想法將不勝感激。 我只是想從變量中追加名稱/值對。 無法獲得附加工作。

這段代碼有幾個問題:

  1. 您正在聲明NSString但是您正在分配NSURL
  2. 你錯過了一個右方括號']'
  3. 你錯過了第二行的雙引號
  4. 您正在嘗試在NSURL對象上調用NSString方法
  5. 你在第一行拼錯了strURI (它是strUIR

嘗試這個:

NSString *strURI = @"https://cid.hooru.mobi:36610/?";
strURI = [strURI stringByAppendingFormat:@"&Cust=IPRR%d", strCust]; 
//Note the %d (if strCust is an int. If it's an NSString use %@)

NSURL *myUrl = [NSURL UrlWithString:strURI];

[NSURL URLWithString:]返回NSURL類型的指針。 僅收集NSString *類型中的NSURL *類型的返回值不會將其轉換為NSString *類型。 因此strUIR是NSURL *類型,即使聲明為NSString strUIR,因此您不能將任何消息傳遞給應該傳遞給NSString類型的strUIR

NSURL不響應stringByAppendingFormat:

您將strURI指定為NSString,因此編譯器不會發出警告,但您將其設置為NSURL。

要么在創建NSURL之前初始化完整的字符串,要么使用URLByAppendingPathComponent:,我建議使用第一個選項。

    NSString *path = @"https://cid.hooru.mobi:36610/?"

...

    path = [path stringByAppendingFormat:@"&Cust=IPRR%@", strCust];
    NSURL *url = [NSURL URLWithString:path]

有關詳細信息,請參閱文檔: https//developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/Reference/Reference.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM