开发者

Returning an array elements using an index

This is the default code that comes part of a page base app . _pageData basically takes a number in and returns a month . I would like to do something quite simple but is giving me grief in Objective C . I would basically like to declare an immutable Array with strings and instead of _pageData converting a number into a month, I would like use this same number as an index in the array and return the matching string .

I hope it makes sense .

EDIT (more code)

@interface ModelController()
@property (readonly, strong, nonatomic) NSArray *pageData;
@end

@implementation ModelController

@synthesize pageData = _pageData;

- (id)init
{
    self = [super init];
    if (self) {
        // Create the data model.
//        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//        _pageData = [[dateFormatter monthSymbols] copy];

        NSArray *items = [NSArray arrayWithObjects:@"one", @"two", @"three", nil];
        NSString *item = [items objectAtIndex:1];
        _pageData = item;


    return self;
}

- (DataViewController *)viewControllerAtIndex:(NSUInteger)index storyboard:(UIStoryboard *)storyboard
{   
    // Return the data view controller for the given index.
    if (([self.pageData count] == 0) || (index >= [self.pageData count])) {
        return nil;
    }

    // Create a new view controller and pass suitable data.
    DataViewController *dataViewController = [storyboard instantiateViewControllerWithIdentifier:@"DataViewController"];

    dataViewController.dataObject = [self.pageData objectAtIndex:index];
    return dataViewCon开发者_运维百科troller;
}


hopefully this snippet helps. You can create an array from static strings and then index into to read one of them.

NSArray *items = [NSArray arrayWithObjects:@"one", @"two", @"three", nil];
NSString *item = [items objectAtIndex:1];
NSLog(@"item=%@", item);

concerning your month example, if it's months be careful about getting it out of a array of static strings. For an example that's fine but for a real app that may get localized, it's better to get it from date objects.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜