lundi 29 juin 2015

How to create variable inside if and else if become global variable?

I have collection view but currently still use variable of some strings :

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    var urlku = NSURL(string: "http://ift.tt/1efOUC2")
    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
}

what I want to do is get that var names become this :

var names: [String] = ["\(judul1)", "\(judul2)", "\(judul3)", "\(judul4)", "\(judul5)", "\(judul6)", "\(judul7)"]

If judul1 to judul7 not create become global variable, it will produce error "use of unresolved identifier", so I need to make judul1 to judul7 become global variable.

How to create judul1 to judul7 become global variable?

Regards.

Aucun commentaire:

Enregistrer un commentaire