Khwan Siricharoenporn
Khwan Siricharoenporn

Reputation: 99

How to Get Value from Array to Variable Swift

I have a Questions I want to move Value in array to Variable ex. [1,2,3] = array i want to get "1" to Variable Var = 1 <= Which "1" is Value in array

My code :

//Loop For Seach Value

for result in 0...DataSearch.count-1 {
    let Object = DataSearch[result] as! [String:AnyObject];

self.IDMachine_Array.append(Object["IDMac"] as! String!);
self.Password_Array.append(Object["password"] as! String!);
self.Conpassword_Array.append(Object["password_con"] as! String!);
self.Tel_Array.append(Object["Tel"] as! String!);
self.Email_Array.append(Object["Email"] as! String!);
self.Email = String(self.Email_Array);
}

I try Value Email = Email_Array Result print :

[[email protected]]

but i want Result is :

[email protected] -> without []

Please Help me please. Thank you. Sorry if my solution is wrong.

Upvotes: 0

Views: 9095

Answers (1)

deanWombourne
deanWombourne

Reputation: 38475

Just get the first element from the array?

self.Email = self.EmailArray.first!

(this is the same as self.Email = self.EmailArray[0])

NB: first! or [0] will both crash if the array is empty. The original question uses as! so obviously just need this to work. However, if you wanted safety you would use something like

if let email as self.EmailArray.first {
    self.Email = email
}

or

self.Email = self.EmailArray.first ?? "no email found"

Upvotes: 1

Related Questions