Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Converting NSData to NSArray

PerdafircaxPerdafircax Posts: 4New Users
edited July 2012 in iPhone SDK Development
Hi,

I'm creating an application which calls data from a MySQL Database.
I've created php files to use to get the information.


Xcode implementation file:

NSString *strURL = [NSString stringWithFormat:@"http://localhost/getCountryArray.php];

// to execute php code
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];

// to receive the returend value
NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];


The above works, but if i change the results i want to an array, it won't work.
My code is as follows:

NSArray *arrResult = [NSKeyedUnarchiver unarchiveObjectWithData:dataURL];



My call statement in my PHPfile is as follows:

$sql="SELECT Category FROM Interest_Category";
$result=mysql_query($sql);

while($row = mysql_fetch_object($result))
{
echo $row->Category;
}


Any help would be appreciated!
Post edited by Perdafircax on

Replies

  • Duncan CDuncan C Posts: 8,149Tutorial Authors, Registered Users
    edited July 2012
    Perdafircax;440847 said:
    Hi,

    I'm creating an application which calls data from a MySQL Database.
    I've created php files to use to get the information.


    Xcode implementation file:

    NSString *strURL = [NSString stringWithFormat:@"http://localhost/getCountryArray.php];

    // to execute php code
    NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];

    // to receive the returend value
    NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];


    The above works, but if i change the results i want to an array, it won't work.
    My code is as follows:

    NSArray *arrResult = [NSKeyedUnarchiver unarchiveObjectWithData:dataURL];



    My call statement in my PHPfile is as follows:

    $sql="SELECT Category FROM Interest_Category";
    $result=mysql_query($sql);

    while($row = mysql_fetch_object($result))
    {
    echo $row->Category;
    }


    Any help would be appreciated!
    You can only convert data to an array using NSKeyedUnarchiver if the data was created using NSKeyedArchiver. Otherwise it won't work, and there's no way to make it work.

    Now, the Core Foundation library that creates property lists is available in a PHP version. You could convert an array to a property list in PHP, send that to iOS, and convert it back to an array.
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
  • RickSDKRickSDK Posts: 668Registered Users
    edited July 2012
    the easiest way to accomplish this is add a record deliminator when outputing, like this:

    echo $row->Category."|";

    and then convert your string to array like this:

    NSArray *list = [strResult componentsSeparatedByString:@"|"];
  • Duncan CDuncan C Posts: 8,149Tutorial Authors, Registered Users
    edited July 2012
    RickSDK;440856 said:
    the easiest way to accomplish this is add a record deliminator when outputing, like this:

    echo $row->Category."|";

    and then convert your string to array like this:

    NSArray *list = [strResult componentsSeparatedByString:@"|"];
    Good point. Keep it simple.
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
Sign In or Register to comment.