Hello Sultans of Swift!Swift NSTextField stringValueに設定値が表示されない
私はC、C++、C#を多用していますが、私はSwiftには新しいです。私は本当に私を困惑させている状況に遭った。私はそれだけで空のスペースを表示する変数にそののstringValueプロパティを設定することで、ラベルのテキストを設定しようとすると
@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)
}
}
}
、まだ私は実際に変数を印刷することができます。私はここにコードスニペットを投稿するつもりですコンソールはうまくいきます。変数は、関数の戻り値として設定されます。変数を明示的に文字列定数に設定すると、それが機能します。これはおそらく関数の戻り値の不確定性に関連していますが、私はそれがコンソールに出力できるので、テキストが含まれていることを知っています。
ここで地球上の何が起こっているのか誰にも気付くことができますか?
おかげ
編集:私はちょうど代わりに、ファイル名のsongTitleを参照するために、コード内のコメントを修正 - 混乱のため申し訳ありません。これはsongTitle.stringValue =タイトル
EDITの設定についてです:これはsongTitleの定義である:これらののstringValueプロパティを設定すると、実際にはあまりにも長い間、私は変数を使用していないと動作しないこと
@IBOutlet weak var fileName: NSTextField!
@IBOutlet weak var songTitle: NSTextField!
は注意mp3File.getTitle()の戻り値が割り当てられます。また、mp3File.getTitle()は値を返し、コンソールにそれを印刷できることに注意してください。
'debugPrint(title)'で何を得ますか? – OOPer
こんにちは、遅れて申し訳ありません - 私は離れていました。ここに私が追加した3つのデバッグステートメントがあります: – daeljan
debugPrint( "DEBUG TEXT") debugPrint(タイトル) debugPrint(songTitle.stringValue) – daeljan