2017-02-13 104 views
0

Swift的Sultans!Swift NSTextField stringValue不顯示設定值

我是新來的Swift,雖然我已經使用C,C++和C#很多。我遇到了令我感到困惑的情況。我要發佈的代碼片段在這裏:

@IBAction func myFunc(sender: AnyObject) 
{ 
    let dirPicker: NSOpenPanel = NSOpenPanel() 
    dirPicker.allowsMultipleSelection = false 
    dirPicker.canChooseFiles = true 
    dirPicker.canChooseDirectories = false 
    dirPicker.runModal() 

    let selection = dirPicker.URL 

    if(selection != nil) 
    { 
     do 
     { 
      print(selection) 
      let mp3File = try MP3File(path: (selection?.path)!) 
      let title = mp3File.getTitle() 

      // This prints OK 
      print("Title:\t\(mp3File.getTitle())") 

      // This prints OK too 
      print("Title:\t\(title)") 

      print("Artist:\t\(mp3File.getArtist())") 
      print("Album:\t\(mp3File.getAlbum())") 
      print("Lyrics:\n\(mp3File.getLyrics())") 

      fileName.stringValue = (selection?.path)! 

      // This sets the label songTitle to an empty space and I can't see why. 
      // If I initialise title to: 
      //  let title = "STRING CONSTANT" 
      // ...instead of 
      //  let title = mp3File.getTitle() 
      // ...then it does actually set the correct text on the label songTitle. 
      // In both cases, printing to the console works fine! Its just setting 
      // the text on the label that is eluding me! 
      songTitle.stringValue = title 
     } 
     catch ID3EditErrors.FileDoesNotExist 
     { 
      print("Error: File Does Not Exist") 
     } 
     catch ID3EditErrors.NotAnMP3 
     { 
      print("Error: Not an MP3") 
     } 
     catch let e 
     { 
      print(e) 
     } 
    } 
} 

當我試圖通過其stringValue的屬性設置爲它只是顯示空的空間變量設置在標籤中的文本,但我實際上可以打印變量控制檯就好了。該變量被設置爲函數的返回值。現在,如果我將變量顯式設置爲一個字符串常量,那麼它就可以工作。所以這可能與函數返回值的不確定性有關,但我知道它包含文本,因爲我可以將它打印到控制檯。

任何人都可以發現這裏發生了什麼?

感謝

編輯:我只是固定在代碼中的註釋是指SONGTITLE而不是文件名 - 遺憾的混亂。這是有關設置songTitle.stringValue =標題

編輯:這是SONGTITLE的定義:

@IBOutlet weak var fileName: NSTextField! 
@IBOutlet weak var songTitle: NSTextField! 

注意,設置這些的stringValue的屬性不實際工作,只要我不使用的變量它被分配了mp3File.getTitle()的返回值。還要注意,mp3File.getTitle()確實會返回一個值,我可以將它打印到控制檯上。

+0

你用'debugPrint(title)'得到了什麼? – OOPer

+0

嗨,抱歉,延遲迴答 - 我不在。下面是我添加的3個調試語句: – daeljan

+0

debugPrint(「DEBUG TEXT」) debugPrint(title) debugPrint(songTitle.stringValue) – daeljan

回答

0

當你有一個字符串值,它是print版罰款:

print(title) //->I Ran 

但不能處理好:

songTitle.stringValue = title //->`songTitle` shows nothing!!! 

(當然,您已確認songTitle是罰款通過分配常數字符串)

一個可能的原因可能是字符串中存在一些控制字符。

您可以使用debugPrint透露這種情況:

debugPrint(title) //->"\0I Ran\0" 

debugPrint使用字符串文字樣的格式顯示控制字符。

在這種情況下,您在兩端都有NUL個字符(U + 0000)。

所以,一個速戰速決的每次修剪他們,你得到這樣的字符串:

斯威夫特2

let title = mp3File.getTitle().stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "\0")) 

斯威夫特3

let title = mp3File.getTitle().trimmingCharacters(in: CharacterSet(charactersIn: "\0")) 

或者你可以寫一個延期,如果你不能碰庫的原始出處:

斯威夫特2

extension MP3File { 
    var title: String { 
     return self.getTitle().stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "\0")) 
    } 
} 

斯威夫特3

extension MP3File { 
    var title: String { 
     return self.getTitle().trimmingCharacters(in: CharacterSet(charactersIn: "\0")) 
    } 
} 

(假設MP3File沒有屬性命名title。)

並將其用作:

let title = mp3File.title 
+0

歡呼聲回答@OOPer – daeljan

+0

很好的答案 - 非常全面! – daeljan