EDIT:コードは正常に動作しています。私はプロジェクトを保存してクリーンアップし、シミュレータをリセットして、今は動作しています!私を助けてくれてありがとう@pbasdf!ローカライズされた文字列からコアデータ値を設定する
私はまだコアデータとローカライズされた文字列の両方に慣れているので、ローカライズされた文字列を間違って使用しているのかどうか、問題が他の場所にあるかどうかはわかりませんが、私のprintステートメントでは、すべての値がnilであり、明らかにWebページはロードされていません。
私はそれに対応するURLに名前を同一視するlocalized.stringsファイルを使用しています:
"iPhone" = "http://www.apple.com/iphone/";
"iPad" = "http://www.apple.com/ipad/";
"Macbook" = "http://www.apple.com/macbook/";
"Google" = "https://www.google.com/";
"Firebase" = "https://firebase.google.com/";
"Magic Leap" = "https://www.magicleap.com/";
"Facebook" = "https://www.facebook.com/";
"Instagram" = "https://www.instagram.com/?hl=en";
"WhatsApp" = "https://www.whatsapp.com/";
"Model S" = "https://www.tesla.com/models";
"Model X" = "https://www.tesla.com/modelx";
"Powerwall" = "https://www.tesla.com/powerwall";
"Twitter" = "https://twitter.com/?lang=en";
"Periscope" = "https://www.periscope.tv/";
"Vine" = "https://vine.co/";
だから私は私のProduct
エンティティの(タイプString
の)url
属性を設定することができます。企業や製品の辞書:
let defaultProducts = ["Apple" : ["iPhone", "iPad", "Macbook"],
"Google" : ["Google", "Firebase", "Magic Leap"],
"Facebook" : ["Facebook", "Instagram", "WhatsApp"],
"Tesla" : ["Model S", "Model X", "Powerwall"],
"Twitter" : ["Twitter", "Periscope", "Vine"]]
そして、ここに私の機能から、関連するコードは次のとおりです。
let companyProducts = defaultProducts[name]
for productName in companyProducts! {
let product = NSManagedObject(entity: productEntity, insertInto:managedContext)
let names = NSLocalizedString(productName, comment:"")
product.setValue(productName, forKey: "name")
product.setValue(productName, forKey: "image")
product.setValue(names, forKey: "url")
product.setValue(company, forKey: "company")
}
その後、私はセルが
// WebView
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let appDelegate =
UIApplication.shared.delegate as? AppDelegate else {
return
}
let managedContext =
appDelegate.persistentContainer.viewContext
let entity =
NSEntityDescription.entity(forEntityName: "Product",
in: managedContext)!
let product = NSManagedObject(entity: entity,
insertInto: managedContext)
if let url = product.value(forKey: "url") as? URL {
webView.load(URLRequest(url: url))
webView.allowsBackForwardNavigationGestures = true
view = webView
}
print(product.value(forKey: "url") as? URL)
}
をタップした際に、Webページを表示するためにそれを使用しますlocalized.strings
に間違って何かをして、nil
という値になっていますか?
EDIT:URL辞書とsetDefaults()
:
func setDefaults() {
let userDefaults = UserDefaults.standard
let defaultValues = ["firstRun" : true]
userDefaults.register(defaults: defaultValues)
if userDefaults.bool(forKey: "firstRun") {
let defaultProducts = ["Apple" : ["iPhone", "iPad", "Macbook"],
"Google" : ["Google", "Firebase", "Magic Leap"],
"Facebook" : ["Facebook", "Instagram", "WhatsApp"],
"Tesla" : ["Model S", "Model X", "Powerwall"],
"Twitter" : ["Twitter", "Periscope", "Vine"]]
let urlDictionary = ["iPhone" : "http://www.apple.com/iphone/",
"iPad" : "http://www.apple.com/ipad/",
"Macbook" : "http://www.apple.com/macbook/",
"Google" : "https://www.google.com/",
"Firebase" : "https://firebase.google.com/",
"Magic Leap" : "https://www.magicleap.com/",
"Facebook" : "https://www.facebook.com/",
"Instagram" : "https://www.instagram.com/?hl=en",
"WhatsApp" : "https://www.whatsapp.com/",
"Model S" : "https://www.tesla.com/models",
"Model X" : "https://www.tesla.com/modelx",
"Powerwall" : "https://www.tesla.com/powerwall",
"Twitter" : "https://twitter.com/?lang=en",
"Periscope" : "https://www.periscope.tv/",
"Vine" : "https://vine.co/"]
let companyEntity = NSEntityDescription.entity(forEntityName: "Company", in: managedContext)!
let productEntity = NSEntityDescription.entity(forEntityName: "Product", in: managedContext)!
// Setting the default company data (name, logo, and stockPrice)
let url = URL(string: "https://query.yahooapis.com/v1/public/yql?q=select%20symbol%2C%20Ask%2C%20YearHigh%2C%20YearLow%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22AAPL%22%2C%22GOOG%22%2C%22TWTR%22%2C%22TSLA%22%2C%20%22FB%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys")!
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
if error != nil {
print(error!)
} else if let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 {
let json = JSON(data: data!)
if let quotes = json["query"]["results"]["quote"].array {
for quote in quotes {
let symbol = quote["symbol"].stringValue
let name = NSLocalizedString(symbol, comment:"")
let ask = quote["Ask"].stringValue
let company = NSManagedObject(entity: companyEntity,insertInto: managedContext)
company.setValue(name, forKey: "name")
company.setValue(name, forKey: "logo")
company.setValue(ask, forKey: "stockPrice")
companies.append(company)
let companyProducts = defaultProducts[name]
for productName in companyProducts! {
let product = NSManagedObject(entity: productEntity, insertInto:managedContext)
let names = urlDictionary[productName]
product.setValue(productName, forKey: "name")
product.setValue(productName, forKey: "image")
product.setValue(names, forKey: "url")
product.setValue(company, forKey: "company")
}
print(json)
}
DispatchQueue.main.async {
do {
try managedContext.save()
vc.tableView.reloadData()
userDefaults.set(false, forKey: "firstRun")
} catch let error as NSError {
print("Could not save. \(error), \(error.userInfo)")
}
}
}
} else {
print("The data couldn't be loaded")
}
}
task.resume()
}
}
を:didSelectRowAt:'新しい(空白)の製品を作成し、そのURLを取得しています属性 - したがってこれはゼロです。代わりに、タップされた行の正しい製品を検索する必要があります: 'let product = products [indexPath.row]'。 – pbasdf
@pbasdfそれは愚かな間違いです、私がそこでやっていたことに気付いたはずです - 私はあなたの提案に変えましたが、私はまだnilを見ています。明らかに、これを行う最も効率的な方法ではないので、私は 'localized.strings'アプローチを取り除こうとしています。おそらくそれが原因でしょうか? – d0xi45
'print(product.value(forKey:" url "))'、それはnilかurlを表示しますか? – pbasdf