0

I receive an object that is sent as a pointer to a collection. I can access the fields directly as an NSDictionary.

The emails field is sent as an array, and I receive it in parentheses. Currently I get the emailaddress by what I feel is a roundabout method. How can I most efficiently get to the email address?

let allDoc = users.allDocuments[0]
print("allDoc: \(allDoc)")

let emailsArray = users.allDocuments[0].valueForKey("emails") as! NSArray
print("emailsArray: \(emailsArray)")

let emailAddress = emailsArray[0]["address"] as! String
print("emailAddress: \(emailAddress)")

Console output:

allDoc: <METDocument key: <collection: users, ID: kzzw3vcqukD62xEyz>, fields: {
    emails =     (
                {
            address = "[email protected]";
            verified = 1;
        }
    );
    profile =     {
        address =         {
            city = "";
            country = nor;
            lineOne = "";
            lineTwo = "";
            zip = "";
        };
        card =         {
            last4 = 4242;
            verified = 1;
        };
        filledOut = 1;
        name =         {
            first = XXXX;
            last = XXXX;
        };
        phone = 9999999999;
        validated = 1;
    };
}>
emailsArray: (
        {
        address = "[email protected]";
        verified = 1;
    }
)
emailAddress: [email protected]
3
  • Update your question with the results of printing users.allDocuments[0]. Commented Oct 31, 2015 at 18:35
  • @rmaddy I have updated Commented Oct 31, 2015 at 18:47
  • It is a Meteor-iOS object Commented Oct 31, 2015 at 18:50

1 Answer 1

1

If you just want the email address you could simply do:

let emailsAddress = users.allDocuments[0]["emails"][0][@"address"] as! String
print("emailAddress: \(emailAddress)")

But it really is better to use multiple lines. It makes the code much easier to read and debug. Also, putting all that onto one line doesn't allow any error checking. What if the @"emails" key has an empty array, for example? The code will crash.

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

Comments

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.