Swift NSTextField stringValue未显示设置值
您好,斯威夫特苏丹 虽然我用C、C++和C语言很多,但我是斯威夫特的新手。我遇到了一个让我非常困惑的情况。我将在这里发布一段代码片段:Swift NSTextField stringValue未显示设置值,swift,macos,Swift,Macos,您好,斯威夫特苏丹 虽然我用C、C++和C语言很多,但我是斯威夫特的新手。我遇到了一个让我非常困惑的情况。我将在这里发布一段代码片段: @IBAction func myFunc(sender: AnyObject) { let dirPicker: NSOpenPanel = NSOpenPanel() dirPicker.allowsMultipleSelection = false dirPicker.canChooseFiles = true dirPic
@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=title的
编辑:这是songTitle的定义:
@IBOutlet weak var fileName: NSTextField!
@IBOutlet weak var songTitle: NSTextField!
请注意,只要我不使用分配了mp3File.getTitle()返回值的a变量,设置这些函数的stringValue属性实际上是可行的。另外请注意,mp3File.getTitle()确实返回了一个值,我可以将其打印到控制台OK。当您有一个字符串值为
print
ed时:
print(title) //->I Ran
但不能很好地处理:
songTitle.stringValue = title //->`songTitle` shows nothing!!!
(当然,您已经通过为songTitle
指定常量字符串来确认它是正确的。)
一个可能的原因可能是字符串中存在一些控制字符
您可以使用debugPrint
显示以下情况:
debugPrint(title) //->"\0I Ran\0"
debugPrint
使用类似字符串文字的格式显示控制字符
在这种情况下,两端都有NUL字符(U+0000)
因此,一个快速解决方法是在每次获得此类字符串时修剪它们:
Swift 2:
let title = mp3File.getTitle().stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "\0"))
let title = mp3File.getTitle().trimmingCharacters(in: CharacterSet(charactersIn: "\0"))
extension MP3File {
var title: String {
return self.getTitle().stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "\0"))
}
}
extension MP3File {
var title: String {
return self.getTitle().trimmingCharacters(in: CharacterSet(charactersIn: "\0"))
}
}
Swift 3:
let title = mp3File.getTitle().stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "\0"))
let title = mp3File.getTitle().trimmingCharacters(in: CharacterSet(charactersIn: "\0"))
extension MP3File {
var title: String {
return self.getTitle().stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "\0"))
}
}
extension MP3File {
var title: String {
return self.getTitle().trimmingCharacters(in: CharacterSet(charactersIn: "\0"))
}
}
如果无法接触库的原始源,也可以编写扩展: Swift 2:
let title = mp3File.getTitle().stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "\0"))
let title = mp3File.getTitle().trimmingCharacters(in: CharacterSet(charactersIn: "\0"))
extension MP3File {
var title: String {
return self.getTitle().stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "\0"))
}
}
extension MP3File {
var title: String {
return self.getTitle().trimmingCharacters(in: CharacterSet(charactersIn: "\0"))
}
}
Swift 3:
let title = mp3File.getTitle().stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "\0"))
let title = mp3File.getTitle().trimmingCharacters(in: CharacterSet(charactersIn: "\0"))
extension MP3File {
var title: String {
return self.getTitle().stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "\0"))
}
}
extension MP3File {
var title: String {
return self.getTitle().trimmingCharacters(in: CharacterSet(charactersIn: "\0"))
}
}
(假设MP3File
没有名为title
的属性)
并将其用作:
let title = mp3File.title
通过
debugPrint(title)
,您得到了什么?您好,很抱歉延迟回答-我不在。下面是我添加的3条调试语句:debugPrint(“调试文本”)debugPrint(title)debugPrint(songtile.stringValue)“\0I run\0”“\0I run\0”为答案@OOPerGreat answer干杯-非常全面!