1

Here is my array which is the response from my backend

NSArray  *myArray = [NSArray arrayWithObjects:@"  userId: 123, online: 0, subscriptionState: none",@"  userId: 124, online: 0, subscriptionState: none",@"  userId: 125, online: 1, subscriptionState: none",nil];
NSLog(@"this is an example array=%@", myArray);]

Log:

this is an example array=(
    "  userId: 123, online: 0, subscriptionState: none",
    "  userId: 124, online: 0, subscriptionState: none",
    "  userId: 125, online: 1, subscriptionState: none"
)

I want to extract only the userID numbers from this array to make a new array that would have a log output like this

this is the new array=(
        "123",
        "124",
        "125"
    )
4
  • 6
    You should probably consider re-factoring your code if you have to rely on text extraction for operations like this.
    – esqew
    Jul 8, 2014 at 15:28
  • *myArray is a response from the backend service I'm using for my app.
    – mostly
    Jul 8, 2014 at 15:49
  • 1
    That's what I had assumed. I meant you should be re-factoring your backend to be using something WAY more structured like JSON for your communications. Simply outputting strings and then relying on offsets or regular expressions to capture the data is VERY unreliable.
    – esqew
    Jul 8, 2014 at 15:50
  • This looks suspiciously like an almost json response. If that is the case, check out NSJSONSerialization or github.com/johnezang/JSONKit Jul 8, 2014 at 16:05

1 Answer 1

2

Try creating a new Class User:NSObject and using an array of Users instead of trying to parse this stuff out. If you're tracking items for each user, then you really need a user class to contain it. This will allow you to use User objects instead of a string that you parse out. This gives you a lot more power, flexibility, and cleaner code.

User.h

@interface User : NSObject

@property (nonatomic) int userId;
@property (nonatomic) BOOL isOnline;
@property (nonatomic) int subscriptionState;

Then in your class:

User *user1 = [User new];
user1.userId = 123;
user1.isOnline = NO;
user1.subscriptionState = 0;
...

NSArray *usersArray = [NSArray arrayWithObjects:user1,user2,user3,nil];
for (User *curUser in usersArray)
{
   NSLog(@"User ID: %d", curUser.userId);
}

Now you can print out if they're online, what their subscriptionState is, and also make updates to a user very easy. Before, you would have to manually update the whole NSString for the user if they changed their subscriptionState or if they came online. For example, if User1 came online, then you would have to reset it to: @" userId: 123, online: 1, subscriptionState: none". This is extremely messy and a lot of work. Now you can just do:

 user1 = [usersArray objectAtIndex:0];
 user1.isOnline = YES;

UPDATE:

It was brought to my attention this is return from a backend. Note this is how to make it work with the backend return if you cannot fix the backend. It is highly unsuggested to use this type of method unless you're forced to.

NSArray *wordArray = [userString componentsSeparatedByString:@","];
NSString *userId = [[[wordArray objectAtIndex:0] componentsSeparatedByString:@":"] objectAtIndex:1];
NSString *online = [[[wordArray objectAtIndex:1] componentsSeparatedByString:@":"] objectAtIndex:1];
NSString *subscriptionStatus = [[[wordArray objectAtIndex:2] componentsSeparatedByString:@":"] objectAtIndex:1];
5
  • I appreciate the detailed response, thank you. The problem is I'm getting back the first array from a backend service I'm using and that is how the data is formatted. I already have classes for users but in order to make an array these particular users I need to first parse all the userIDs from the response. This is the really looong version of the question stackoverflow.com/questions/24596452/…
    – mostly
    Jul 8, 2014 at 15:46
  • Oh, gotcha. Didn't realize this was from a server return (be sure to specify that in the question, otherwise we assume you're new to programming) :). Looks like this is the return from Quickblox - is there no way to get JSON? If it's like this then you'll need to split your array twice, I'll update the question in a second Jul 8, 2014 at 15:48
  • I'm sure you are correct LyricalPanda, I just have no idea how to work with JSON as I am rookie and don't have experience with JSON. I don't really understand the documentation for using it. I'm using Quickblox but I don't see anything specifically about what I'm trying to do. Here is some json for other classes quickblox uses though if that may be a clue quickblox.com/developers/Users
    – mostly
    Jul 8, 2014 at 15:58
  • Wow your a real wiz! thanks man. This should do for now hopefully but I definitely need to learn more about using JSON, thanks for the support!
    – mostly
    Jul 8, 2014 at 15:59
  • I just looked at their documentation, they're actually returning JSON format. I'm not sure why your code is returning a parsed out NSString. I'll take a look at your other question later and see if I can figure out why Jul 8, 2014 at 16:00

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.