M Swapnil
M Swapnil

Reputation: 2381

Putting value from an array in swift?

I have a table view in which i want to put row names from my array's object.

I have declared an array

var myArray = [1,2,3,4,5,6,7]

in my tableView how can I put value from this array.I didn't find ObjectAtIndex method, so I am doing this

myLabel.text= String(format: "%@",array [ indexPath.row])

Upvotes: 0

Views: 100

Answers (5)

Leo Dabus
Leo Dabus

Reputation: 236360

You have two mistakes there. First you have declared myArray not array. Second when using integers you have to use %d. Try like this:

let myArray = [1,2,3,4,5,6,7]

myLabel.text = String(format: "%d", myArray[indexPath.row])

Upvotes: 1

Ramkumar chintala
Ramkumar chintala

Reputation: 958

You can write like this also:

myLabel.text = String.localizedStringWithFormat(" %.02f %.02f %.02f", r, g, b)

Upvotes: 0

Dharmesh Kheni
Dharmesh Kheni

Reputation: 71854

Your myArray is swift Array of type [Int] and it is not NSArray, swift array doesn't have property ObjectAtIndex so you can access element from swift array with index this way:

var myArray = [1,2,3,4,5,6,7]
myLabel.text = "\(myArray[indexPath.row])"

And myArray[indexPath.row] will return Int because myArray type is [Int].

If you want to assign text to myLabel then you have to convert that to String from Int and you can do that by string interpolation.

Upvotes: 1

Antonio
Antonio

Reputation: 72760

The easiest way is using string interpolation:

myLabel.text = "\(array[indexPath.row])"

If you want to know more, consult the official documentation

Upvotes: 1

Özgür Ersil
Özgür Ersil

Reputation: 7013

You can do it in this way.

myLabel.text= String(stringInterpolationSegment: myArray[indexPath.row]!)

Upvotes: 1

Related Questions