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()確實會返回一個值,我可以將它打印到控制檯上。
你用'debugPrint(title)'得到了什麼? – OOPer
嗨,抱歉,延遲迴答 - 我不在。下面是我添加的3個調試語句: – daeljan
debugPrint(「DEBUG TEXT」) debugPrint(title) debugPrint(songTitle.stringValue) – daeljan