如何在 if 和 else if 内部创建变量成为全局变量?
How to create variable inside if and else if become global variable?
我有集合视图,但目前仍在使用一些字符串的变量:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
var urlku = NSURL(string: "http://json.comli.com/example.json")
var data = NSData(contentsOfURL: urlku!)
if let json: NSDictionary = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers, error: nil) as? NSDictionary {
if let dict = json as? [String: AnyObject] {
println(dict)
if let weatherDictionary = dict["data"]! as? [AnyObject] {
println("\nWeather dictionary:\n\n\(weatherDictionary)")
if let descriptionString = weatherDictionary[0]["challengeName"]! as? String {
println("\nDescription of the weather is: \(descriptionString)")
var judul1 = "\(descriptionString)"
}else if let descriptionString1 = weatherDictionary[1]["challengeName"]! as? String {
println("\nDescription of the weather is: \(descriptionString1)")
var judul2 = "\(descriptionString1)"
}else if let descriptionString2 = weatherDictionary[1]["currentPhaseName"]! as? String {
println("\nDescription of the weather is: \(descriptionString2)")
var judul3 = "\(descriptionString2)"
}else if let descriptionString3 = weatherDictionary[1]["currentPhaseRemainingTime"]! as? String {
println("\nDescription of the weather is: \(descriptionString3)")
var judul4 = "\(descriptionString3)"
}else if let descriptionString4 = weatherDictionary[0]["challengeType"]! as? String {
println("\nDescription of the weather is: \(descriptionString4)")
var judul5 = "\(descriptionString4)"
}else if let descriptionString5 = weatherDictionary[0]["challengeId"]! as? String {
println("\nDescription of the weather is: \(descriptionString5)")
var judul6 = "\(descriptionString5)"
}else if let descriptionString6 = weatherDictionary[1]["currentStatus"]! as? String {
println("\nDescription of the weather is: \(descriptionString6)")
var judul7 = "\(descriptionString6)"
}
}
}
}
var names: [String] = ["Lorem ipsum dolor sit er elit lamet, Lorem ipsum dolor", "Lorem ipsum dolor sit er elit lamet, Lorem ipsum dolor", "Lorem ipsum dolor sit er elit lamet, Lorem ipsum dolor", "Lorem ipsum dolor sit er elit lamet, Lorem ipsum dolor", "Lorem ipsum dolor sit er elit lamet, Lorem ipsum dolor", "Lorem ipsum dolor sit er elit lamet, Lorem ipsum dolor", "Lorem ipsum dolor sit er elit lamet, Lorem ipsum dolor", "Lorem ipsum dolor sit er elit lamet, Lorem ipsum dolor", "Lorem ipsum dolor sit er elit lamet, Lorem ipsum dolor", "Lorem ipsum dolor sit er elit lamet, Lorem ipsum dolor", "Lorem ipsum dolor sit er elit lamet, Lorem ipsum dolor", "Lorem ipsum dolor sit er elit lamet, Lorem ipsum dolor", "Lorem ipsum dolor sit er elit lamet, Lorem ipsum dolor", "Lorem ipsum dolor sit er elit lamet, Lorem ipsum dolor"]
// Create the cell and return the cell
var cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! ImageUICollectionViewCell
// Add image to cell
cell.image.image = model.images[indexPath.row]
cell.labelku.text = names[indexPath.row]
cell.labelku.backgroundColor = UIColor(white: 0.5, alpha: 0.5)
return cell
}
我想要做的是让 var 名称变成这样:
var names: [String] = ["\(judul1)", "\(judul2)", "\(judul3)", "\(judul4)", "\(judul5)", "\(judul6)", "\(judul7)"]
如果judul1到judul7不创建成为全局变量,会报错"use of unresolved identifier",所以我需要让judul1到judul7成为全局变量
如何创建judul1到judul7成为全局变量?
此致。
您不能将函数变量用作全局变量,因为它们仅限于该函数,但您可以通过在 class 声明上方声明它来创建全局变量,如下所示:
import UIKit
var judul1 = ""
var judul7 = ""
class ViewController: UIViewController {
//Your code
}
现在这将使 judul1
和 judul7
全局化,现在您可以在 cellForItemAtIndexPath
方法中为 in 赋值,例如:
judul1 = "\(descriptionString)"
judul7 = "\(descriptionString6)"
并且您可以在项目的任何位置访问它。
另一种方法是您可以使用 NSUserDefaults
将这两个变量存储到磁盘中,如下面的 cellForItemAtIndexPath
方法所示:
对于judul1
:
if let descriptionString = weatherDictionary[0]["challengeName"]! as? String {
println("\nDescription of the weather is: \(descriptionString)")
var judul1 = "\(descriptionString)"
NSUserDefaults.standardUserDefaults().setObject(judul1, forKey: "judul1")
}
对于judul7
:
else if let descriptionString6 = weatherDictionary[1]["currentStatus"]! as? String {
println("\nDescription of the weather is: \(descriptionString6)")
var judul7 = "\(descriptionString6)"
NSUserDefaults.standardUserDefaults().setObject(judul7, forKey: "judul7")
}
之后,您可以在任何地方访问它,例如:
let judul1: AnyObject? = NSUserDefaults.standardUserDefaults().objectForKey("judul1")
let judul7: AnyObject? = NSUserDefaults.standardUserDefaults().objectForKey("judul7")
希望对您有所帮助。
我有集合视图,但目前仍在使用一些字符串的变量:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
var urlku = NSURL(string: "http://json.comli.com/example.json")
var data = NSData(contentsOfURL: urlku!)
if let json: NSDictionary = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers, error: nil) as? NSDictionary {
if let dict = json as? [String: AnyObject] {
println(dict)
if let weatherDictionary = dict["data"]! as? [AnyObject] {
println("\nWeather dictionary:\n\n\(weatherDictionary)")
if let descriptionString = weatherDictionary[0]["challengeName"]! as? String {
println("\nDescription of the weather is: \(descriptionString)")
var judul1 = "\(descriptionString)"
}else if let descriptionString1 = weatherDictionary[1]["challengeName"]! as? String {
println("\nDescription of the weather is: \(descriptionString1)")
var judul2 = "\(descriptionString1)"
}else if let descriptionString2 = weatherDictionary[1]["currentPhaseName"]! as? String {
println("\nDescription of the weather is: \(descriptionString2)")
var judul3 = "\(descriptionString2)"
}else if let descriptionString3 = weatherDictionary[1]["currentPhaseRemainingTime"]! as? String {
println("\nDescription of the weather is: \(descriptionString3)")
var judul4 = "\(descriptionString3)"
}else if let descriptionString4 = weatherDictionary[0]["challengeType"]! as? String {
println("\nDescription of the weather is: \(descriptionString4)")
var judul5 = "\(descriptionString4)"
}else if let descriptionString5 = weatherDictionary[0]["challengeId"]! as? String {
println("\nDescription of the weather is: \(descriptionString5)")
var judul6 = "\(descriptionString5)"
}else if let descriptionString6 = weatherDictionary[1]["currentStatus"]! as? String {
println("\nDescription of the weather is: \(descriptionString6)")
var judul7 = "\(descriptionString6)"
}
}
}
}
var names: [String] = ["Lorem ipsum dolor sit er elit lamet, Lorem ipsum dolor", "Lorem ipsum dolor sit er elit lamet, Lorem ipsum dolor", "Lorem ipsum dolor sit er elit lamet, Lorem ipsum dolor", "Lorem ipsum dolor sit er elit lamet, Lorem ipsum dolor", "Lorem ipsum dolor sit er elit lamet, Lorem ipsum dolor", "Lorem ipsum dolor sit er elit lamet, Lorem ipsum dolor", "Lorem ipsum dolor sit er elit lamet, Lorem ipsum dolor", "Lorem ipsum dolor sit er elit lamet, Lorem ipsum dolor", "Lorem ipsum dolor sit er elit lamet, Lorem ipsum dolor", "Lorem ipsum dolor sit er elit lamet, Lorem ipsum dolor", "Lorem ipsum dolor sit er elit lamet, Lorem ipsum dolor", "Lorem ipsum dolor sit er elit lamet, Lorem ipsum dolor", "Lorem ipsum dolor sit er elit lamet, Lorem ipsum dolor", "Lorem ipsum dolor sit er elit lamet, Lorem ipsum dolor"]
// Create the cell and return the cell
var cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! ImageUICollectionViewCell
// Add image to cell
cell.image.image = model.images[indexPath.row]
cell.labelku.text = names[indexPath.row]
cell.labelku.backgroundColor = UIColor(white: 0.5, alpha: 0.5)
return cell
}
我想要做的是让 var 名称变成这样:
var names: [String] = ["\(judul1)", "\(judul2)", "\(judul3)", "\(judul4)", "\(judul5)", "\(judul6)", "\(judul7)"]
如果judul1到judul7不创建成为全局变量,会报错"use of unresolved identifier",所以我需要让judul1到judul7成为全局变量
如何创建judul1到judul7成为全局变量?
此致。
您不能将函数变量用作全局变量,因为它们仅限于该函数,但您可以通过在 class 声明上方声明它来创建全局变量,如下所示:
import UIKit
var judul1 = ""
var judul7 = ""
class ViewController: UIViewController {
//Your code
}
现在这将使 judul1
和 judul7
全局化,现在您可以在 cellForItemAtIndexPath
方法中为 in 赋值,例如:
judul1 = "\(descriptionString)"
judul7 = "\(descriptionString6)"
并且您可以在项目的任何位置访问它。
另一种方法是您可以使用 NSUserDefaults
将这两个变量存储到磁盘中,如下面的 cellForItemAtIndexPath
方法所示:
对于judul1
:
if let descriptionString = weatherDictionary[0]["challengeName"]! as? String {
println("\nDescription of the weather is: \(descriptionString)")
var judul1 = "\(descriptionString)"
NSUserDefaults.standardUserDefaults().setObject(judul1, forKey: "judul1")
}
对于judul7
:
else if let descriptionString6 = weatherDictionary[1]["currentStatus"]! as? String {
println("\nDescription of the weather is: \(descriptionString6)")
var judul7 = "\(descriptionString6)"
NSUserDefaults.standardUserDefaults().setObject(judul7, forKey: "judul7")
}
之后,您可以在任何地方访问它,例如:
let judul1: AnyObject? = NSUserDefaults.standardUserDefaults().objectForKey("judul1")
let judul7: AnyObject? = NSUserDefaults.standardUserDefaults().objectForKey("judul7")
希望对您有所帮助。