2015-06-30 74 views
1

我收集的看法,但目前仍然使用一些字符串變量:如何在if和else中創建變量如果成爲全局變量?

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 names: [String] = ["\(judul1)", "\(judul2)", "\(judul3)", "\(judul4)", "\(judul5)", "\(judul6)", "\(judul7)"] 

如果judul1到judul7無法創建成爲全球變量,它會產生錯誤「使用未解析的標識符」,所以我需要使judul1成爲全局變量。

如何創建judul1到judul7成爲全局變量?

問候。

回答

0

不能使用函數的變量爲全局變量,因爲它們用的是功能有限的,但你可以通過聲明它上面的類聲明在全球創建一個變量,就像圖所示:

import UIKit 

var judul1 = "" 
var judul7 = "" 

class ViewController: UIViewController { 
    //Your code 
} 

現在這個意願使judul1judul7全球,現在你可以在你的cellForItemAtIndexPath方法就像在賦值:

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") 

希望它能幫助你。

+0

隨時upvote,如果它可以幫助你.. :) –