0

I did save value to an object mapper called var testing:[Test]? By using this code

let test: Test = Test()! 


var i = 0
    while i < self.array.count{
        let test: Test = Test()!
        test.userId = self.array[i]
        print(test.userId)
        test.firstName = self.array[i+1]
        test.lastName = self.array[i+2]
        test.img1 = self.array[i+3]
        test.img2 = self.array[i+4]
        test.img3 = self.array[i+5]
        test.featuredImg1 = self.array[i+6]
        test.url1 = self.array[i+7]
        test.featuredImg2 = self.array[i+8]
        test.url2 = self.array[i+9]
        test.featuredImg3 = self.array[i+10]
        test.url3 = self.array[i+11]
        test.userId2 = self.array[i+12]
        test.firstName2 = self.array[i+13]
        test.lastName2 = self.array[i+14]
        test.img4 = self.array[i+15]
        test.img5 = self.array[i+16]
        test.img6 = self.array[i+17]
        test.featuredImg4 = self.array[i+18]
        test.url4 = self.array[i+19]
        test.featuredImg5 = self.array[i+20]
        test.url5 = self.array[i+21]
        test.featuredImg6 = self.array[i+22]
        test.url6 = self.array[i+23]

        i = i + 24


        testing?.append(test)

    }
    print(testing?.count)

Which is not working. test.userId print is giving a value inside the loop but when i print the count the value is nil , apparently the values are not being saved correctly.

4
  • There is no declaration of testing array also let test: Test = Test()! should be inside the for loop. Commented Feb 24, 2017 at 6:50
  • Declaration of testing at the top of the page : var testing:[Test]? Commented Feb 24, 2017 at 6:54
  • Is that testing array is an Test array? and do you append test data at the bottom of for loop? Can we see the whole for loop if posible Commented Feb 24, 2017 at 6:55
  • 1
    You are definitely using too many exclamation and question marks. The array seems to contain non-optional strings. If you declare the array as [String]() you get rid of all type casts, too. Commented Feb 24, 2017 at 7:21

1 Answer 1

1

I don't know variable s is relevant with your problem but I think it should work like this

 var testing:[Test]?

 for i in 0 ..< self.array.count{

    let test: Test = Test()
    test.userId = self.array[0] as! String?
    print(test.userId)
    test.firstName = self.array[1] as! String?
    test.lastName = self.array[2] as! String?
    test.img1 = self.array[3] as! String?
    test.img2 = self.array[4] as! String?
    test.img3 = self.array[5] as! String?
    test.featuredImg1 = self.array[6] as! String?
    test.url1 = self.array[7] as! String?
    test.featuredImg2 = self.array[8] as! String?
    test.url2 = self.array[9] as! String?
    test.featuredImg3 = self.array[10] as! String?
    test.url3 = self.array[11] as! String?
    test.userId2 = self.array[12] as! String?
    test.firstName2 = self.array[13] as! String?
    test.lastName2 = self.array[14] as! String?
    test.img4 = self.array[15] as! String?
    test.img5 = self.array[16] as! String?
    test.img6 = self.array[17] as! String?
    test.featuredImg4 = self.array[18] as! String?
    test.url4 = self.array[19] as! String?
    test.featuredImg5 = self.array[20] as! String?
    test.url5 = self.array[21] as! String?
    test.featuredImg6 = self.array[22] as! String?
    test.url6 = self.array[23] as! String?

    self.s = self.s! + 24

    //this is where your array is filled with test data.
    testing.append(test)

}

Test Class :

class Test
{
    var userId: String?
    var firstName: String?
    var lastName: String?
    var img1: String?
    var img2: String?
    var img3: String?
    var featuredImg1: String?
    var url1: String?
    var featuredImg2: String?
    var url2: String?
    var featuredImg3: String?
    var url3: String?
    var userId2: String?
    var firstName2: String?
    var lastName2: String?
    var img4: String?
    var img5: String?
    var img6: String?
    var featuredImg4: String?
    var url4: String?
    var featuredImg5: String?
    var url5: String?
    var featuredImg6: String?
    var url6: String?

    init() {

    }


}
Sign up to request clarification or add additional context in comments.

8 Comments

Did not work. I dont know if the problem is in saving the values.
are you sure your self.array is not empty and your test variable gets the right data?
The array is empty , i checked again and testing.append(test) is not saving the values.
@yasserh then your test variable is empty. did you check the count of testing array. It is zero or same as self.array.count but empty elements?
the test array count is 48 , test.anyvalue is not empty , testing array is empty
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.