赋予它们所有可选值

时间:2016-09-01 08:20:07

标签: ios swift variables

你好我有变量,但给出了所有变量$mp3。我如何在下面解决他们的代码。

Json附加下面Optional() koltuklar数组的代码,您可以看到

koltuklaridler

在下面你可以看到代码里面的tableview(代码在koltuklaridler里面搜索id之后做了开放的koltuklar索引路径项目,当发现从它获取一些变量时)

        for name in json as! [AnyObject] {

            let SeatName = name["SeatName"]
            let SeatDesignId = name["SeatDesignId"]
            self.koltuklar.append("\(SeatName)*\(SeatDesignId)*")

            if let blogs = json["SeatDetail"] as? [[String: AnyObject]] {
                for blog in blogs {

                    let TicketTypeId = blog["TicketTypeId"]
                    let TicketTypeName = blog["TicketTypeName"]
                    let Amount = blog["Amount"]

                    self.koltuklaridler.append("\(SeatDesignId)*\(TicketTypeId)*\(TicketTypeName)*\(Amount)*")


                }

            }

也是我的json文件

var koltuklar = [""]
var koltuklaridler = [""]

if let myStrings:String! = koltuklar[indexPath.row]{

    print("\(myStrings!)")

    let myStringArrf = myStrings.componentsSeparatedByString("*")


    print("\(myStringArrf)")


    if let koltukisims:String! = String(myStringArrf[0]) {

        cell.koltukName.text = koltukisims
    }


    print(" STR - \(myStringArrf[1] as String!)")


    if let index = koltuklaridler.indexOf(myStringArrf[1] as String!) {

        let myStringdetaysecilen = koltuklaridler[index]

        print("myStringdetaysecilen \(myStringdetaysecilen)")


    }

输出

[
    {
        "SeatDesignId": 16484,
        "SeatName": "A6",
        "SaloonId": 148,
        "SeatDetail": [
            {
                "TicketTypeId": 1,
                "TicketTypeName": "Okay",
                "Amount": 13
            }
        ]
    },

所有变量输出可选我尝试了一切,但没有修复。

3 个答案:

答案 0 :(得分:0)

你的问题是你是从dict的值创建一个字符串而没有if let语句所以它返回一个可选的值:

在json中的名字为! [AnyObject] {

        if let SeatName = name["SeatName"],
           let SeatDesignId = name["SeatDesignId"] {
            self.koltuklar.append("\(SeatName)*\(SeatDesignId)*")
        }

        if let blogs = json["SeatDetail"] as? [[String: AnyObject]] {
            for blog in blogs {

                if let TicketTypeId = blog["TicketTypeId"],
                   let TicketTypeName = blog["TicketTypeName"],
                   let Amount = blog["Amount"] {
                   self.koltuklaridler.append("\(SeatDesignId)*\(TicketTypeId)*\(TicketTypeName)*\(Amount)*")
                }
            }

        }

答案 1 :(得分:0)

正如我的评论中所提到的,无论何时使用字符串插值"\(...)",都要确保所有可选字符串都已解包。从字典中读取的值始终是可选的

此代码解包所有可选字符串

for name in json as! [[String:AnyObject]] {

  guard let SeatName = name["SeatName"] as? String,
        SeatDesignId = name["SeatDesignId"] as? Int else  {
      continue
  }
  self.koltuklar.append("\(SeatName)*\(SeatDesignId)*")

  if let blogs = json["SeatDetail"] as? [[String: AnyObject]] {
    for blog in blogs {
      if let TicketTypeId = blog["TicketTypeId"] as? Int,
           TicketTypeName = blog["TicketTypeName"]  as? String,
                   Amount = blog["Amount"]  as? Int {
        self.koltuklaridler.append("\(SeatDesignId)*\(TicketTypeId)*\(TicketTypeName)*\(Amount)*")        
      }
    }
}

编辑:我根据JSON

将转换更新为实际类型

现在将两个数组声明为空字符串数组。

var koltuklar = [String]()
var koltuklaridler = [String]()

并删除第一行中的可选绑定

let myStrings = koltuklar[indexPath.row]
print("\(myStrings)")
...

顺便说一句:你用星号“序列化”字符串并在表格视图中反序列化的方式是非常非常笨拙和低效的。使用自定义类或结构来存储数据记录。

答案 2 :(得分:0)

有两种操作方式可选。

  
      
  1. 使用“!”解开但是,如果价值为零,则存在崩溃的可能性。
  2.   
  3. 使用“if let”条件使用术语调用“可选绑定”解包。
  4.   
if let var = "assigned your optional variable"{
print(var)
}

您将获得没有可选项的变量。