xcode - Int 到 String Swift

标签 xcode swift macos

我制作了一个计数器,并设法保存和加载该数据。问题是“var counter = 0”,所以当我向上按计数器(+1)而不是转到下一个数字时,它会返回到 0,因为变量设置为 0。这是我的代码

class ReminderClass: NSViewController {


@IBOutlet var Count: NSTextField!

  override func viewDidLoad() {
    readStats()
    super.viewDidLoad()

}

func readStats() -> (NSString?){
    let location = NSString(string:"location to my file").stringByExpandingTildeInPath
    let fileContent = try? NSString(contentsOfFile: location, encoding: NSUTF8StringEncoding)
    print(fileContent)
    Count.stringValue = (fileContent?.stringByExpandingTildeInPath)!
    fileContent?.stringByReplacingOccurrencesOfString(Count.stringValue, withString: Count.stringValue)
    return fileContent
}

var counter = 0 //thats the problem
var counterfind = ""

@IBAction func PlusOne(sender: AnyObject) {
    counter = counter + 1
    Count.stringValue = NSString(format: "%i", counter) as String
}

@IBAction func MinusOne(sender: AnyObject) {
    counter = counter - 1
    Count.stringValue = NSString(format: "%i", counter) as String
}

@IBAction func save(sender: AnyObject) {
    print(Count.stringValue)
    let data = NSString(string:Count.stringValue)
    let destPath = "path to my file"
    _ = NSFileManager.defaultManager()
    do {
        try data.writeToFile(destPath, atomically: true, encoding: NSUTF8StringEncoding)
    } catch let error as NSError {
        print("Error: \(error)")
    }

    print(counter)
    self.view.window?.close()

}

最佳答案

感谢大家帮助我解决我的第一个 Mac 应用程序遇到的小问题。我设法找出问题所在。现在这是我的代码:

class ReminderClass: NSViewController {




@IBOutlet var Count: NSTextField!

  override func viewDidLoad() {
    readStats()
    super.viewDidLoad()

}



func readStats() -> (NSString?){
    let location = NSString(string:"path-to-file").stringByExpandingTildeInPath
    let fileContent = try? NSString(contentsOfFile: location, encoding: NSUTF8StringEncoding)
    print(fileContent)
    Count.stringValue = (fileContent?.stringByExpandingTildeInPath)!
    fileContent?.stringByReplacingOccurrencesOfString(Count.stringValue, withString: Count.stringValue)
    return fileContent
}



    @IBAction func PlusOne(sender: AnyObject) {
    let counter : String = Count.stringValue
    var num : Int? = Int(counter)
    num = num! + 1
    Count.stringValue = NSString(format: "%i", num!) as String
    }

@IBAction func MinusOne(sender: AnyObject) {
    let counter : String = Count.stringValue
    var num : Int? = Int(counter)
    num = num! - 1
    Count.stringValue = NSString(format: "%i", num!) as String
    }


@IBAction func save(sender: AnyObject) {
    print(Count.stringValue)
    let data = NSString(string:Count.stringValue)
    let destPath = "path-to-file"
    _ = NSFileManager.defaultManager()
    do {
        try data.writeToFile(destPath, atomically: true, encoding: NSUTF8StringEncoding)
    } catch let error as NSError {
        print("Error: \(error)")
    }

    self.view.window?.close()

}

再次感谢大家! :)

关于xcode - Int 到 String Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37149351/

相关文章:

ios - 有没有办法手动更改 PDF 的 url 路径并使其实时更新?

ios - 内存泄漏,即使每个 alloc 都已释放

ios - 如何在 Swift 和 Alamofire 中将对象映射到对象内部的数组中

swift - 错误 : No valid registration IDs iOS FCM

Xcode 6.1 对 git 的表现很奇怪

ios - SFSpeechRecognizer 多种语言

bash - macOS上的curl与unicode字符的问题

objective-c - 应用程序沙箱:无法解析文档范围的书签;不返回任何错误

ruby - 在带有 RVM : 的 OS X 上安装 Ruby 1.9.3 时出现 "No available formula for gcc46"

ios - 如何更改来自不同结构的@State 变量