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

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

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

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.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, 02:47 PM   #1 (permalink)
Registered Member
 
Join Date: Dec 2009
Location: Benicia, CA
Age: 50
Posts: 152
That Don Guy is on a distinguished road
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, 03:54 PM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
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 - Professional iOS App Development. Available for hire.

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

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 03-20-2010, 08:49 PM   #3 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 342
flamingliquid is on a distinguished road
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: 350
11 members and 339 guests
akacaj, c2matrix, cgokey, esoteric, givensur, HemiMG, Mirotion22, mjnafjke, Pudding, SLIC, Techgirl-52
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,651
Threads: 94,115
Posts: 402,887
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Mirotion22
Powered by vBadvanced CMPS v3.1.0

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