When setting up the path, the error similar to the comment in the code below appears.I don't know the cause, so if anyone knows, please let me know.
import UIKit
classViewController:UIViewController {
let paths = NSSearchPathForDirectoriesInDomains(
.DocumentDirectory,
.UserDomainMask, true)
let documentsPath=paths[0]
// Instance member 'paths' cannot be used in type 'ViewController
// The error appears.
let path=(paths[0]as NSSstring).stringByAppendingPathComponent("datastore.plist")
print(paths)
var user: NSDictionary = [
"Name": "A",
"Age": "B",
"Sex": "C",
]
let success=user.writeToFile(path, atomically:true)
// Instance member 'user' cannot be used in type 'ViewController
// The error appears.
// print(success)
if success {
print("success!")
} else{
print("failure!")
}
override func viewDidLoad(){
super.viewDidLoad()
// Do any additional setup after loading the view, typically from anib.
}
override funcdidReceiveMemoryWarning(){
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I will explain in detail later.For now, please try to build the program below as copy and pace.Then, on the simulator, press the button Save and then press the button Read.
import UIKit
classViewController:UIViewController {
vartextView —UITextView!
override func viewDidLoad(){
super.viewDidLoad()
let saveButton = UIButton ( frame : CGRect ( x:20.0 , y : 80.0, width : 120.0, height : 44.0 )
saveButton.setTitleColor (saveButton.tintColor, forState:UIControlState.Normal)
saveButton.setTitle("Save", forState:UIControlState.Normal)
saveButton.addTarget(self, action: "saveDictionary:", forControlEvents:UICControlEvents.TouchUpInside)
self.view.addSubview(saveButton)
let readButton = UIButton (frame: CGRect (x:22.0, y:80.0, width:120.0, height:44.0))
readButton.setTitleColor (saveButton.tintColor, forState:UIControlState.Normal)
readButton.setTitle("Read", forState:UIControlState.Normal)
readButton.addTarget(self, action: "readFile:", forControlEvents:UICControlEvents.TouchUpInside)
self.view.addSubview(readButton)
textView = UITextView (frame: CGRect (x:20.0, y:140.0, width:280.0, height:280.0))
textView.backgroundColor=UIColor.lightGrayColor()
textView.editable=false
self.view.addSubview(textView)
}
func saveDictionary (sender: AnyObject) {
let paths = NSSearchPathForDirectoriesInDomains(
.DocumentDirectory,
.UserDomainMask, true)
let documentsPath=paths[0] as NSSstring
let path=documentsPath.stringByAppendingPathComponent("datastore.plist")
print(paths)
let user: NSDictionary = [
"Name": "A",
"Age": "B",
"Sex": "C",
]
let success=user.writeToFile(path, atomically:true)
// print(success)
if success {
print("success!")
} else{
print("failure!")
}
}
funcreadFile(sender:AnyObject){
let paths = NSSearchPathForDirectoriesInDomains(
.DocumentDirectory,
.UserDomainMask, true)
let documentsPath=paths[0] as NSSstring
let path=documentsPath.stringByAppendingPathComponent("datastore.plist")
let theDict = NSDictionary (contentsOfFile:path)
if theDict!=nil{
textView.text="\(theDict!)"
} else{
print("Failure")
}
}
}
Can you see that it is not a "path configuration error" but an error that occurred 100 steps before that?
When you write a program in the form of a class, there is a rule that you write a program for this purpose everywhere.To change the expression, it's "rule.If you write something that is out of the rules, a warning will appear saying, "You cannot build it as it is."It's a red error message.
The questioner says, "Because I don't know the cause," but the reason is that I didn't follow the rules.To avoid errors, first learn the rules for programming classes.The error message is based on the assumption that there is still something wrong with the rule, so I did not point out the wrong part directly.
Let's leave the precise "rules" description to a specialized book and show a typical class description pattern here.
class class name:inheriting superclass name {
Instance variable (properties)
Initiator (init())
Method (func method name ( ))
}
For subclasses of UIViewController
, it is recommended that the initialization process be written to the viewDidLoad()
method, not the initiator (of course, it is not unnecessary).
If there is a program you want to run in a test, it is recommended that you implement it in the action method of the button.The previous sample program is an example.Press the button to run the program you want to try.
© 2024 OneMinuteCode. All rights reserved.