gpt4 book ai didi

xcode - Int 到 String Swift

转载 作者:行者123 更新时间:2023-11-30 10:03:50 25 4
gpt4 key购买 nike

我制作了一个计数器,并设法保存和加载该数据。问题是“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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com