0

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.

1
  • 1
    I would get rid of all those forced casts and properly test each one. If something ever goes wrong with any of them you'll have a nasty runtime crash that will be difficult to figure out. Commented Jun 25, 2017 at 19:41

1 Answer 1

1

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"
Sign up to request clarification or add additional context in comments.

3 Comments

yes i want to get first element in array,I will try now.
FYI - Of course this will crash if the array is empty.
@rmaddy Yep - and this code would fail peer review anywhere I worked :) However, the question contains as! on pretty much every line, so I've assumed that the op is looking for a quick fix. I'll add that warning anyway just so anyone else looking at the answer is warned :)

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.