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