2017-02-13 105 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