How to add/insert element in array one after another like stack swift 5

前端 未结 2 903
情书的邮戳
情书的邮戳 2021-01-24 06:02

I get String from api response and stored in an array.

     for customParams in custom_attributes! {
         if(customParams[\"attribute_code\"] as! String == \         


        
相关标签:
2条回答
  • 2021-01-24 06:28
    var myArray :[String] = []
    
    for customParams in custom_attributes! {    
        if(customParams["attribute_code"] as! String == "small_image") {
             var myString = customParams["value"] as! String
             myArray.append(myString)
        }
    }
    
    0 讨论(0)
  • 2021-01-24 06:42

    You need to loop through the items array first as it is in json, also declare myArray outside the for loop and start appending myString inside myArray as shown:

    var myArray :[String] = []
    for custom_attributes! in items { // items is the json items array
        for customParams in custom_attributes! {
            if(customParams["attribute_code"] as! String == "small_image") 
                 {
                 print(customParams["value"])
                 var myString = customParams["value"] as! String
                 myArray.append(myString)
    
             }
         }
     }
    
    0 讨论(0)
提交回复
热议问题