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 10-04-2011, 08:34 PM   #1 (permalink)
Registered Member
 
Join Date: Aug 2011
Posts: 12
Peak Mobile is on a distinguished road
Default NSArray Troubles

Hi everyone,

I'm having trouble initializing an array, populating a UITextView with an object from the array (all of the objects are NSStrings) and then being able to switch which object is displayed with a next and a previous button. Here is my code:

The header file:
Code:
#import <UIKit/UIKit.h>

@interface WordsPage : UIViewController {
    IBOutlet UITextView *wordsText;
    IBOutlet UIButton *prevButton;
    IBOutlet UIButton *nextButton;
    NSArray *WordsArray;
    
}

@property (retain, nonatomic) UITextView *wordsText;
@property (retain, nonatomic) UIButton *prevButton;
@property (retain, nonatomic) UIButton *nextButton;
@property (retain, nonatomic) NSArray *WordsArray;

-(IBAction)nextButton:(id)sender;
-(IBAction)prevButton:(id)sender;
-(void)updateText;

@end

And my implemenation file:
Code:
#import "WordsPage.h"

@implementation WordsPage
@synthesize wordsText;
@synthesize prevButton;
@synthesize nextButton;
@synthesize WordsArray;



int num;

NSString *words1 = @"Words string 1";
NSString *words2 = @"Words string 2";
NSString *words3 = @"Words string 3";
NSString *words4 = @"Words string 4";
NSString *words5 = @"Words string 5";
NSString *words6 = @"Words string 6";

//ERROR Initializer element is not a compile-time constant
static NSArray *WordsArray = [[NSArray alloc] initWithObjects:
                        words1,
                        words2,
                        words3,
                        words4,
                        words5,
                        words6, nil];   //ERROR Initializer element is not a constant

    
    
-(IBAction)prevButton:(id)sender {
    num--;
    if (num<0) num=5;
    [self updateText];
    
}


-(IBAction)nextButton:(id)sender {
    num++;
    if (num>5) num=0;
    [self updateText];
    
}

-(void)updateText {
        wordsText.text = [WordsArray objectAtIndex:num];
}

- (void)viewDidLoad {

    
    int num = 0;
    
    wordsText.text = [WordsArray objectAtIndex:num];
    
    self.navigationItem.title=@"Words Page ";
    [super viewDidLoad];
}


-(void)dealloc {
    [wordsText release];
    [favButton release];
    [nonfavButton release];
    [WordsArray release];
    [super dealloc];
}

@end
Any ideas of what I am doing wrong?

Thanks!

-Andrew
Peak Mobile is offline   Reply With Quote
Old 10-04-2011, 08:51 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Peak Mobile View Post
Hi everyone,

I'm having trouble initializing an array, populating a UITextView with an object from the array (all of the objects are NSStrings) and then being able to switch which object is displayed with a next and a previous button. Here is my code:

The header file:
Code:
#import <UIKit/UIKit.h>

@interface WordsPage : UIViewController {
    IBOutlet UITextView *wordsText;
    IBOutlet UIButton *prevButton;
    IBOutlet UIButton *nextButton;
    NSArray *WordsArray;
    
}

@property (retain, nonatomic) UITextView *wordsText;
@property (retain, nonatomic) UIButton *prevButton;
@property (retain, nonatomic) UIButton *nextButton;
@property (retain, nonatomic) NSArray *WordsArray;

-(IBAction)nextButton:(id)sender;
-(IBAction)prevButton:(id)sender;
-(void)updateText;

@end

And my implemenation file:
Code:
#import "WordsPage.h"

@implementation WordsPage
@synthesize wordsText;
@synthesize prevButton;
@synthesize nextButton;
@synthesize WordsArray;



int num;

NSString *words1 = @"Words string 1";
NSString *words2 = @"Words string 2";
NSString *words3 = @"Words string 3";
NSString *words4 = @"Words string 4";
NSString *words5 = @"Words string 5";
NSString *words6 = @"Words string 6";

//ERROR Initializer element is not a compile-time constant
static NSArray *WordsArray = [[NSArray alloc] initWithObjects:
                        words1,
                        words2,
                        words3,
                        words4,
                        words5,
                        words6, nil];   //ERROR Initializer element is not a constant

    
    
-(IBAction)prevButton:(id)sender {
    num--;
    if (num<0) num=5;
    [self updateText];
    
}


-(IBAction)nextButton:(id)sender {
    num++;
    if (num>5) num=0;
    [self updateText];
    
}

-(void)updateText {
        wordsText.text = [WordsArray objectAtIndex:num];
}

- (void)viewDidLoad {

    
    int num = 0;
    
    wordsText.text = [WordsArray objectAtIndex:num];
    
    self.navigationItem.title=@"Words Page ";
    [super viewDidLoad];
}


-(void)dealloc {
    [wordsText release];
    [favButton release];
    [nonfavButton release];
    [WordsArray release];
    [super dealloc];
}

@end
Any ideas of what I am doing wrong?

Thanks!

-Andrew
The values assigned to static variables have to be constants.

Make wordsArray a retained property, not a static var, and initialize it in your init method.

use code like this:

Code:
self.wordsArray = [NSArray arrayWithObjects: 
  words1,
  words2,
  words3,
  words4,
  words5,
  words6,
  nil];
and make sure to set self.wordsArray to nil in your dealloc method.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 10-04-2011, 09:25 PM   #3 (permalink)
Registered Member
 
Join Date: Aug 2011
Posts: 12
Peak Mobile is on a distinguished road
Default

Aren't I retaining the array from the @property line of code in the header file? How do I retain it from the implementation file?

And can you explain the init method? I have not seen that before. Or are you refering to the ViewDidLoad method?

Thanks,

Andrew

Last edited by Peak Mobile; 10-04-2011 at 09:51 PM.
Peak Mobile is offline   Reply With Quote
Old 10-04-2011, 10:24 PM   #4 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

1. No, you aren't using your property. You are using whole entirely different variable and to top it off, you made it a constant.

2. The init method he posted does the exact same thing as initWithObjects: except it returns an autoreleased object instead of one with a retain count of 1.
__________________
If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.

New app - See screenshots and details at www.globaclock.com.

If you want to thank me, click the link. Every click counts. If you want to do more, buy my app. A link is available on my website. Thanks.
Domele is offline   Reply With Quote
Old 10-22-2011, 08:53 AM   #5 (permalink)
Registered Member
 
Join Date: Aug 2011
Posts: 12
Peak Mobile is on a distinguished road
Default

It took me forever, but I finally got it working! Thank you for your help, guys!
Peak Mobile is offline   Reply With Quote
Reply

Bookmarks

Tags
element, error, initializer, nsarray, nsstring

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: 395
17 members and 378 guests
13dario13, 7twenty7, eski, EvilElf, glenn_sayers, HemiMG, iOS.Lover, jarv, LunarMoon, n00b, pbart, Pudding, sacha1996, Sami Gh, UMAD, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,672
Threads: 94,122
Posts: 402,906
Top Poster: BrianSlick (7,990)
Welcome to our newest member, yuncarl28
Powered by vBadvanced CMPS v3.1.0

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