15

assume the following:

@interface BOOK : NSObject
{
    @private
    NSString *title;
    NSString *author;
    NSString *ISBN;
}
...
BOOK *booklist = [[[BOOK alloc] init] autorelease];
NSMutableArray *myarray = [NSMutableArray array];
while (true)
{
    booklist.title = @"title";
    booklist.author = @"author";
    booklist.ISBN = @"1234-1";
    [myarray addObject:booklist];
}

my question is how do I retrieve object of BOOK i.e booklist.title, .author, .ISBN at a certain index in myarray.

4 Answers 4

34

with this code? Impossible, because you add the same bookList for the rest of the time (until there is no more memory)

With code without infinite loops you would use

NSInteger index = 0;
Book *aBook = [myArray objectAtIndex:index];
NSString *title = aBook.title;
Sign up to request clarification or add additional context in comments.

8 Comments

In the real scenario the while loop is a text stream, so while the text stream is true. I declared myarray in the .h file and the .m file I have some functions that call myarray. after assigning the values, I called [myarray ObjectAtIndex:index] but I get a "SIGABRT" in xcode.
if you don't post the real code I can't help. If you get an error or exception you should post the exact code that caused that exception.
this is the exact lineBook *aBook = [myArray objectAtIndex:index];
this is the exact line Book *aBook = [myArray objectAtIndex:0]; actually I get EXC_BAD_ACCESS. the code is quite much.
no error when I call myarray from the same function I populate it but when I call from another function to get the value at a certain index I get the error above.
|
3

Starting with Xcode 4.5 (and Clang 3.3), you may use Objective-C Literals:

Book *firstBookInArray = myArray[0];

Comments

2

This may not help at all, but I remember having trouble when not casting while pulling objects out of arrays...

Book *aBook = (Book *)[myArray objectAtIndex: index];

It may help, may not...and I know this post is old, but maybe it'll help someone..

.scott

1 Comment

Yes. This has been a must do in the past. I believe it returns (id) and casting it helps.
0

You have this extends if you want

Create file extends.h add this code and #import "extends.h" in your project :

/*______________________________ Extends NSMutableArray ______________________________*/
/**
 * Extend NSMutableArray
 * By DaRkD0G
 */
@interface NSMutableArray (NSArrayAdd)
/**
 *  Get element at index
 *
 *  @param index
 */
- (NSObject *) getAt:(int) index;
@end
/**
 * Extend NSMutableArray Method
 * By DaRkD0G
 */
@implementation NSMutableArray (NSArrayAdd)
/**
 *  Get element at index
 *
 *  @param index
 */
- (NSObject *) getAt:(int) index {
    NSInteger anIndex = index;
    NSObject *object = [self objectAtIndex:anIndex];
    if (object == [NSNull null]) {
        return nil;
    } else {
        NSLog(@"OK found ");
        return object;
    }

}
@end
/*______________________________ Extends NSArray ______________________________*/
/**
 * Extend NSArray
 * By DaRkD0G
 */
@interface NSArray (NSArrayAdd)
/**
 *  Get element at index
 *
 *  @param index
 */
- (NSObject *) getAt:(int) index;
@end
/**
 * Extend NSArray Method
 * By DaRkD0G
 */
@implementation NSArray (NSArrayAdd)
/**
 *  Get element at index
 *
 *  @param index
 */
- (NSObject *) getAt:(int) index {
    NSInteger anIndex = index;
    NSObject *object = [self objectAtIndex:anIndex];
    if (object == [NSNull null]) {
        return nil;
    } else {
        NSLog(@"OK found ");
        return object;
    }

}
@end

USE :

NSObject object = [self.arrayItem getAt:0];
NSObject object2 = [self.arrayItem getAt:50];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.