Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-20-2010, 03:47 PM   #1 (permalink)
Registered Member
 
Join Date: Dec 2009
Location: Benicia, CA
Age: 49
Posts: 147
Default Converting an int[] to an NSArray?

I have an array of integers (i.e. an int[]) that I would like to saved into NSUserDefaults. However, when I try this:
Code:
// "defaults" is an NSUserDefaults variable, initialized to NSStandardUserDefaults
// int savingValues[100] has its values set dynamically in code
	
NSArray *savingArray = [[NSArray alloc] initWithObjects:savingValues count:100];
[defaults setObject:savingArray forKey:@"thisApp_Defaults"];
I get a "passing argument 1 from incompatible pointer type" warning in my initWithObjects call, and the values aren't saved correctly.
Isn't the intent of initWithObjects:count: to be able to create an NSArray from a C array? (Or does "C array" not include "Objective-C array"?)

Pretty much the same thing happens when I copy savingValues into an NSMutableArray and then try to create savingArray from that using initWithArray).

-- Don
That Don Guy is offline   Reply With Quote
Old 03-20-2010, 04:54 PM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,129
Default

A quick perusal of the documentation for that method indicates that the contents of the C array should be objects. They absolutely need to be objects for the NSArray.
__________________
BriTer Ideas LLC - Code review, consulting, development. PM for pricing.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
BrianSlick is offline   Reply With Quote
Old 03-20-2010, 09:49 PM   #3 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 341
Default

Code:
int savingValues[100];
NSMutableArray *savingArray = [[NSMutableArray alloc] initWithCapacity:100];

for (int i = 1; i < 101; i++) {
      NSNumber *n = [[NSNumber alloc] initWithInt:savingValues[i]];
      [savingArray addObject:n];
      [n release];
}
[defaults setObject:savingArray forKey:@"thisApp_Defaults"];
Untested, but it works in my mind :P
flamingliquid is offline   Reply With Quote
Reply

Bookmarks

Tags
nsarray

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 348
21 members and 327 guests
ADY, Dani77, Duncan C, e2applets, Herbie, JasonR, keeshux, linkmx, mer10, Monstertaco, piesia, prchn4christ, Promo Dispenser, Robiwan, sebasx, sly24, Touchmint, twerner, zulfishah
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,880
Threads: 89,228
Posts: 380,760
Top Poster: BrianSlick (7,129)
Welcome to our newest member, @sandris
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:34 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0