Ios 如何使用自定义名称保存和加载.txt文件?
我在一个应用程序中有一个弹出文本框警报,用户可以输入字符为.txt文件创建自定义名称,然后保存该文件。此文件名存储在名为rideContent的全局变量中。调用saveRide()函数时,一个包含字符串信息的文件存储在.txt中,应该使用用户在文本框中创建的文件名,但是存储在数组中的文件名为空 我在stopAction函数中调用save函数的顺序是否错误,或者调用得太早,这就是数组为空的原因?还是我以错误的方式调用rideName=rideContent?i、 e.rideName=rideContent[-1] 我希望用户输入自定义名称,并将其用作保存和加载文件的标识符。任何帮助都将不胜感激 这里我调用一个名为rideContent的全局变量:Ios 如何使用自定义名称保存和加载.txt文件?,ios,arrays,swift,textbox,text-files,Ios,Arrays,Swift,Textbox,Text Files,我在一个应用程序中有一个弹出文本框警报,用户可以输入字符为.txt文件创建自定义名称,然后保存该文件。此文件名存储在名为rideContent的全局变量中。调用saveRide()函数时,一个包含字符串信息的文件存储在.txt中,应该使用用户在文本框中创建的文件名,但是存储在数组中的文件名为空 我在stopAction函数中调用save函数的顺序是否错误,或者调用得太早,这就是数组为空的原因?还是我以错误的方式调用rideName=rideContent?i、 e.rideName=rideCo
var rideContent = [String]()
@IBAction func stopAction(sender: UIButton) {
let alert = UIAlertController(title: "Ride Stopped", message: "Give a title to your ride", preferredStyle: .Alert)
let saveAction = UIAlertAction(title: "Save", style: .Default,
handler: { (action:UIAlertAction) -> Void in
// Allow for text to be added and appended into the RideTableViewController
let textField = alert.textFields!.first
rideContent.append(textField!.text!)
// Update permanent storage after deleting a ride
NSUserDefaults.standardUserDefaults().setObject(rideContent, forKey: "rideContent")
// Segue to the Ride details page
self.performSegueWithIdentifier("ShowRideDetail", sender: nil)
})
let cancelAction = UIAlertAction(title: "Cancel",
style: .Default) { (action: UIAlertAction) -> Void in
}
alert.addTextFieldWithConfigurationHandler {
(textField: UITextField) -> Void in
}
alert.addAction(saveAction)
alert.addAction(cancelAction)
//Show the alert
presentViewController(alert, animated: true, completion: nil)
// Stop the timer
timer.invalidate()
// Stop location updates
self.stopLocation()
// Save the ride
saveRide()
}
func saveRide() {
let rideName = rideContent
// Print Check
print("\(rideName) : the name of the saved ride")
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory = paths[0]
let fileName = ("\(documentsDirectory)")+("\(rideName)")+(".txt")
print("\(fileName)This is firt check")
var content: String
// Adding elasped time information to the bicycle ride
// Content Order: Seconds, Minutes, Hours, Distance \n coordinates
content = ("\(seconds) Seconds, ")+("\(minutes) Minutes, ")+("\(hours) Hours, ") + ("\(self.distanceLabel.text!)") + "\n"
// Create a for loop to add each recorded coordinate value to the content list to later be added and displayed as a polyline
for coord in self.coords {
content = content + ("\(coord.latitude) " + "," + "\(coord.longitude) " + "\n")
}
do{
try content.writeToFile(fileName, atomically: false, encoding: NSUTF8StringEncoding)
}catch _ {
}
}
这里我调用save函数:
var rideContent = [String]()
@IBAction func stopAction(sender: UIButton) {
let alert = UIAlertController(title: "Ride Stopped", message: "Give a title to your ride", preferredStyle: .Alert)
let saveAction = UIAlertAction(title: "Save", style: .Default,
handler: { (action:UIAlertAction) -> Void in
// Allow for text to be added and appended into the RideTableViewController
let textField = alert.textFields!.first
rideContent.append(textField!.text!)
// Update permanent storage after deleting a ride
NSUserDefaults.standardUserDefaults().setObject(rideContent, forKey: "rideContent")
// Segue to the Ride details page
self.performSegueWithIdentifier("ShowRideDetail", sender: nil)
})
let cancelAction = UIAlertAction(title: "Cancel",
style: .Default) { (action: UIAlertAction) -> Void in
}
alert.addTextFieldWithConfigurationHandler {
(textField: UITextField) -> Void in
}
alert.addAction(saveAction)
alert.addAction(cancelAction)
//Show the alert
presentViewController(alert, animated: true, completion: nil)
// Stop the timer
timer.invalidate()
// Stop location updates
self.stopLocation()
// Save the ride
saveRide()
}
func saveRide() {
let rideName = rideContent
// Print Check
print("\(rideName) : the name of the saved ride")
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory = paths[0]
let fileName = ("\(documentsDirectory)")+("\(rideName)")+(".txt")
print("\(fileName)This is firt check")
var content: String
// Adding elasped time information to the bicycle ride
// Content Order: Seconds, Minutes, Hours, Distance \n coordinates
content = ("\(seconds) Seconds, ")+("\(minutes) Minutes, ")+("\(hours) Hours, ") + ("\(self.distanceLabel.text!)") + "\n"
// Create a for loop to add each recorded coordinate value to the content list to later be added and displayed as a polyline
for coord in self.coords {
content = content + ("\(coord.latitude) " + "," + "\(coord.longitude) " + "\n")
}
do{
try content.writeToFile(fileName, atomically: false, encoding: NSUTF8StringEncoding)
}catch _ {
}
}
以下是保存到文本文件的代码:
var rideContent = [String]()
@IBAction func stopAction(sender: UIButton) {
let alert = UIAlertController(title: "Ride Stopped", message: "Give a title to your ride", preferredStyle: .Alert)
let saveAction = UIAlertAction(title: "Save", style: .Default,
handler: { (action:UIAlertAction) -> Void in
// Allow for text to be added and appended into the RideTableViewController
let textField = alert.textFields!.first
rideContent.append(textField!.text!)
// Update permanent storage after deleting a ride
NSUserDefaults.standardUserDefaults().setObject(rideContent, forKey: "rideContent")
// Segue to the Ride details page
self.performSegueWithIdentifier("ShowRideDetail", sender: nil)
})
let cancelAction = UIAlertAction(title: "Cancel",
style: .Default) { (action: UIAlertAction) -> Void in
}
alert.addTextFieldWithConfigurationHandler {
(textField: UITextField) -> Void in
}
alert.addAction(saveAction)
alert.addAction(cancelAction)
//Show the alert
presentViewController(alert, animated: true, completion: nil)
// Stop the timer
timer.invalidate()
// Stop location updates
self.stopLocation()
// Save the ride
saveRide()
}
func saveRide() {
let rideName = rideContent
// Print Check
print("\(rideName) : the name of the saved ride")
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory = paths[0]
let fileName = ("\(documentsDirectory)")+("\(rideName)")+(".txt")
print("\(fileName)This is firt check")
var content: String
// Adding elasped time information to the bicycle ride
// Content Order: Seconds, Minutes, Hours, Distance \n coordinates
content = ("\(seconds) Seconds, ")+("\(minutes) Minutes, ")+("\(hours) Hours, ") + ("\(self.distanceLabel.text!)") + "\n"
// Create a for loop to add each recorded coordinate value to the content list to later be added and displayed as a polyline
for coord in self.coords {
content = content + ("\(coord.latitude) " + "," + "\(coord.longitude) " + "\n")
}
do{
try content.writeToFile(fileName, atomically: false, encoding: NSUTF8StringEncoding)
}catch _ {
}
}
是用户可以输入自定义名称的屏幕截图。根据用户输入的骑乘名称,骑乘名称是否应在打印检查中称为“上午骑乘”
这是在我的打印检查语句[]中打印到控制台的内容:保存的骑乘的名称您的“saveRide()”函数将立即被调用。将其放在块内,以便在您的saveAction中调用。干杯!它让我称之为self.self.saveRide()。给赛尔夫打两次电话常见吗?我以前从未见过。。。