我正在使用导航中心通过视图控制器发送数据。但是我想在按钮内使用此数据,但无法识别该变量。
这是未被识别的当前标识。
class ViewController10: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITextFieldDelegate, UITextViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(forName: ID_TRANSFER, object: nil, queue: nil){ notification in
if let object = notification.object as? [String: Any] {
if let currentid = object["UserID"] as? Int {
print(currentid, "crap it works")
}
}
}
enterPrice1.delegate = self
enterBrand.delegate = self
enterProduct1.delegate = self
enterComment.delegate = self
var fileUrl = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent("RateappDatabase4.sqlite")
if sqlite3_open(fileUrl.path, &db) != SQLITE_OK{
print("Error opening database")
print(fileUrl = try!
FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent("RateappDatabase4.sqlite"))
}
if sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS Rate (RateID INTEGER PRIMARY KEY AUTOINCREMENT, rating INTEGER, UserID INTEGER, ProductId INTEGER, PostID INTEGER)", nil, nil, nil) != SQLITE_OK {
let errmsg = String(cString: sqlite3_errmsg(db)!)
print("error creating table: \(errmsg)")
}
if sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS Product (ProductID INTEGER PRIMARY KEY AUTOINCREMENT, product TEXT, UserID INTEGER, brand TEXT, price INTEGER, PostID INTEGER)", nil, nil, nil) != SQLITE_OK {
let errmsg = String(cString: sqlite3_errmsg(db)!)
print("error creating table: \(errmsg)")
}
if sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS Comment (CommentID INTEGER PRIMARY KEY AUTOINCREMENT, comment TEXT, UserID INTEGER, orderCom INTEGER, PostID INTEGER)", nil, nil, nil) != SQLITE_OK {
let errmsg = String(cString: sqlite3_errmsg(db)!)
print("error creating table: \(errmsg)")
}
if sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS Post (PostID INTEGER PRIMARY KEY AUTOINCREMENT, ProductID INTEGER, UserID INTEGER, RateID INTEGER, CommentID INTEGER, Post TEXT)", nil, nil, nil) != SQLITE_OK {
let errmsg = String(cString: sqlite3_errmsg(db)!)
print("error creating table: \(errmsg)")
} // Do any additional setup after loading the view.
imagePicker.delegate = self
imagePicker.sourceType = .camera
imagePicker.allowsEditing = false
}
@IBOutlet weak var enterPrice1: UITextField!
@IBOutlet weak var enterComment: UITextField!
@IBOutlet weak var enterBrand: UITextField!
@IBOutlet weak var enterProduct1: UITextField!
@IBAction func lipstick1(_ sender: Any) {
var stmt: OpaquePointer?
let queryString3 = "INSERT INTO Rate (rating, UserID) VALUES (?, ?)"
let lipstickValue1 = 1
if sqlite3_prepare(db, queryString3, -1, &stmt, nil) != SQLITE_OK{
let errmsg = String(cString: sqlite3_errmsg(db)!)
print("error preparing insert: \(errmsg)")
return
}
if sqlite3_bind_int(stmt, 1, Int32(lipstickValue1)) != SQLITE_OK{
let errmsg = String(cString: sqlite3_errmsg(db)!)
print("failure inserting lipstick1: \(errmsg)")
return
}
if sqlite3_bind_int(stmt, 2, Int32(currentid)) != SQLITE_OK{
let errmsg = String(cString: sqlite3_errmsg(db)!)
print("failure inserting lipstick1: \(errmsg)")
return
}
if sqlite3_step(stmt) != SQLITE_DONE {
let errmsg = String(cString: sqlite3_errmsg(db)!)
print("failure binding lipstick1: \(errmsg)")
return
}
print("rating of 1")
}
}
答案 0 :(得分:0)
在类的开头声明变量,并为其提供可选值,然后在检索时分配该值。
Future<String> _loadFeedAsset() async {
return await rootBundle.loadString('assets/json/userfeed1.json');
}
Future<Feed> loadFeed() async {
String jsonFeed = await _loadFeedAsset();
final jsonResponse = json.decode(jsonFeed);
Feed feed = new Feed.fromJson(jsonResponse);
//print(feed.feedEntries.length);
return feed;
}
class Data {
Feed feed;
Data(this.feed);
}
void main async {
var feed = await loadFeed();
var data = Data(feed);
print(data.feed.feedEntries.length);
}