Call an array from object - arrays

-(NSMutableArray *) botanicalPlant {
if (_botanicalPlant == nil) {
_botanicalPlant = [[NSMutableArray alloc] initWithObjects:#"Abelia", nil];
}
return _botanicalPlant;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.botanicalPlantName = [[BotanicalPlant alloc] init];
self.botanicalPlantNameLabel.text = []
}
I know this may be a simple question but Im stuck on this one. I have an array in NSObject of Botanical I just don't know how to call it in the viewDidLoad method for it to show up in my main view controller. I don't know what to put in the brackets to assign it to the text of the label.

You haven't shown enough code to answer your question for sure. But if botanicalPlant is a mutable array property on the BotanicalPlant class, then you could do something like:
BotanicalPlant *myBotanicalPlant = [[BotanicalPlant alloc] init];
NSMutableArray *namesArray = myBotanicalPlant.botanicalPlant;
self.botanicalPlantNameLabel.text = [namesArray firstObject];

Try This:
In your viewDidLoad method
NSMutableArray *arrayFromBotanicalMethod = [self botanicalPlant];
// Then set the text label from your new array that you created
self.botanicalPlantNameLabel.text = [arrayFromBotanicalMethod objectAtIndex:0];
This code is based on the limited information you provided. You basically create another array that will hold the returned array from your botanicalPlant method. Then you use the objectAtIndex message on the new array to get the text for the label.
Hope it helps.

Related

changing up some code in 'DraggableViewBackground.m'

Github Source URL: https://github.com/cwRichardKim/RKSwipeCards
So, I need to change up some code in 'DraggableViewBackground.m'.
rather than an array of text as is on the stock source code, I need to create an array of images...
Stock section:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[super layoutSubviews];
[self setupView];
exampleCardLabels = [[NSArray alloc]initWithObjects:#"first",#"second",#"third",#"fourth",#"last", nil]; //%%% placeholder for card-specific information
loadedCards = [[NSMutableArray alloc] init];
allCards = [[NSMutableArray alloc] init];
cardsLoadedIndex = 0;
[self loadCards];
}
return self;
}
I have looked at various implementations of image array, but still comes up with some errors what I can't resolve.
this is my edited code for the array:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[super layoutSubviews];
[self setupView];
exampleCardGraphics = [[NSArray alloc]initWithObjects:00_quote#2x.png, 01_quote#2x.png,02_quote#2x.png,03_quote#2x.png,04_quote#2x.png,05_quote#2x.png,06_quote#2x.png,07_quote#2x.png,08_quote#2x.png,09_quote#2x.png,10_quote#2x.png,11_quote#2x.png,12_quote#2x.png,13_quote#2x.png,14_quote#2x.png,15_quote#2x.png,16_quote#2x.png,17_quote#2x.png,18_quote#2x.png,19_quote#2x.png,20_quote#2x.png,21_quote#2x.png,22_quote#2x.png,23_quote#2x.png,24_quote#2x.png,25_quote#2x.png,26_quote#2x.png,27_quote#2x.png,28_quote#2x.png,29_quote#2x.png,30_quote#2x.png,31_quote#2x.png,32_quote#2x.png,33_quote#2x.png,34_quote#2x.png,35_quote#2x.png,36_quote#2x.png,37_quote#2x.png,38_quote#2x.png,39_quote#2x.png,40_quote#2x.png,41_quote#2x.png,42_quote#2x.png,43_quote#2x.png,44_quote#2x.png,45_quote#2x.png,46_quote#2x.png,47_quote#2x.png,48_quote#2x.png,nil]; //%%% placeholder for card-specific information
loadedCards = [[NSMutableArray alloc] init];
allCards = [[NSMutableArray alloc] init];
cardsLoadedIndex = 0;
[self loadCards];
}
return self;
}
I get returned "Invalid suffix '_quote' on integer constant"
How would I resolve this to get images pulled into the array, and subsequently one image for each swipe card ?
I have changed a few other things, however only instance names, which have been changed accordingly in corresponding files, so the build still succeeds but I can't get the image array to work.
Please help!
Thanks.

strange behavior with postNotificationName with NSDictionary in userInfo

I'm trying to debug a strange problem I'm seeing when passing a NSDictionary as my userInfo in NSNotification using ARC on ios 6. Here's my relevant code:
to send the notification:
NSDictionary *feedData = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&jsonParsingError];
NSDictionary* finalData = [[NSDictionary alloc] initWithObjectsAndKeys:#"index", [NSNumber numberWithInt:i], #"myKey", feedData, nil];
[[NSNotificationCenter defaultCenter] postNotificationName:#"myNotification" object: nil userInfo:finalData];
my handler registration:
[[NSNotificationCenter defaultCenter] addObserver:frcvc selector:#selector(mySelector:) name:#"myNotification" object:NULL];
my handler declaration:
-(void)mySelector:(NSNotification*)notification;
my handler definition which is inside my MyCollectionViewController class:
- (void)mySelector:(NSNotification*) notification
{
NSDictionary* myDict = (NSDictionary*)notification.userInfo;
int index = [[myDict objectForKey:#"index"] integerValue];
NSDictionary* myFeed = (NSDictionary*)[myDict objectForKey:#"myKey"];
}
When I run the code, I can see finalData being constructed, and I make note of the memory address. When I get to my callback and extract userInfo, it is the same memory address, but xcode seems to think it is of type MyCollectionViewController*. And when I try to access index or myFeed, it is null. But in my output window, I can type
"p myDict"
and it correctly shows NSDictionary* with the same memory address as when constructed! And if I type
"po myDict"
it shows the correct dictionary keys and values! What is going on?! I've tried rebuilding clean, but that didn't help. The memory seems to be fine, since I'm getting the correct address and can seem to access it in the debug window. Thanks for any help!
Your objects and keys are in the wrong order:
NSDictionary* finalData = [[NSDictionary alloc] initWithObjectsAndKeys:#"index", [NSNumber numberWithInt:i], #"myKey", feedData, nil];
It should be:
NSDictionary* finalData = [[NSDictionary alloc] initWithObjectsAndKeys: [NSNumber numberWithInt:i], #"index", feedData, #"myKey", nil];
Or even better, using Objective-C Literals:
NSDictionary* finalData = #{#"index": [NSNumber numberWithInt:i], #"myKey": feedData};

Is this an appropriate way of implementing an array of NSImages?

I have a series of images that I want to make use of in an app. What I wanted to do is store them in an NSArray for easy identification within the drawRect: function.
I have created a .plist file, which details an NSDictionary with the keys simply ascending integer values, and the values corresponding to the image file names. This allows me to iterate through the dictionary and add a series of NSImage objects into the array. I am using a for loop here because the order is important and fast enumeration does not guarantee order of execution when reading from a dictionary!
Currently I am doing the following: (This is in a Subclass of NSView)
#property (strong) NSDictionary *imageNamesDict;
#property (strong) NSMutableArray *imageArray;
...
// in init method:
_imageNamesDict = [[NSDictionary alloc] initWithContentsOfFile:#"imageNames.plist"];
_imageArray = [[NSMutableArray alloc] initWithCapacity:[_imageNamesDict count]];
for (int i=0; i<_imageNamesDict.count; i++) {
NSString *key = [NSString stringWithFormat:#"%d", i];
[_imageArray addObject:[NSImage imageNamed:[_imageNamesDict objectForKey:key]];
}
// When I want to draw a particular image in drawRect:
int imageToDraw = 1;
// Get a pointer to the necessary image:
NSImage *theImage = [_imageArray objectAtIndex:imageToDraw];
// Draw the image
NSRect theRect = NSMakeRect (100,100, 0, 0);
[theImage drawInRect:theRect fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
This all appears to work properly, but with one quirk. I have noticed a small amount of lag that happens in drawing the display, but only when drawing a new image for the first time. Once each image has been seen at least once, I can re-draw any desired image with no lag whatsoever.
Is it that I am not loading the images properly, or is there some way that I can pre-cache each image when I create the objects to add to the for loop?
Thanks!
Assuming you have overcome the "... NSMutableDictionary instead of an NSMutableArray ..." problem pointed out in the comments, you are loading the images properly.
The lag you are describing is because [NSImage imageNamed: ] doesn't do all the work required to draw the image, so that is happening on your first draw.
You could probably get rid of the lag by drawing the images into an offscreen buffer as you add them to your array, something like:
// Create an offscreen buffer to draw in.
newImage = [[NSImage alloc] initWithSize:imageRect.size];
[newImage lockFocus];
for (int i=0; i<_imageNamesDict.count; i++) {
NSString *key = [NSString stringWithFormat:#"%d", i];
NSImage *theImage = [NSImage imageNamed:[_imageNamesDict objectForKey:key]];
[_imageArray addObject: theImage];
[theImage drawInRect:imageRect fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
}
[newImage unlockFocus];
[newImage release];

CoreData leaks with fetched results arrays

I'm using CoreData to store objects like cars, trips, data recorded from GPS, etc.
When I fetch what I want to show a list of trips, some stats for a trip, or add a new car in my settings view controller, I use pretty much this kind of request:
- (void)getDataTrip
{
// Fetched data trips.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"DataTrip" inManagedObjectContext:[self managedObjectContext]];
[fetchRequest setEntity:entity];
// Set predicate and sort orderings...
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"timestamp" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
[sortDescriptors release];
[sortDescriptor release];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"idTrip = %#", self.idTrip];
[fetchRequest setPredicate:predicate];
// Execute the fetch -- create a mutable copy of the result.
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[self.managedObjectContext executeFetchRequest:fetchRequest error:&error] mutableCopy];
if (mutableFetchResults == nil) {
// Handle the error.
NSLog(#"failed with error: %#", error);
}
// Set the array.
[self setDataTripArray:mutableFetchResults];
// Memory management.
[fetchRequest release];
[mutableFetchResults release];
}
Sometimes, I have leaks when I do the [self setDataTripArray:mutableFetchResults]; and sometimes not. In this case, when I get the data for a trip, it leaks all the time when I use the navigation controller to come back to the root view controller (displaying a list of trips), and/or when I change tab.
Anyway, it just leaks and it's all the time coming from fetching data from CoreData, and give this array to my local array var.
Please let me know if you see how to fix this! It made the app crash after a while.
Thanks!
SOLUTION
I found that I do a retain on my object dataTripArray object when creating another UIViewController that I use to create graphs for my scroll view.
- (void)loadScrollViewWithPage:(int)page
{
if (page < 0)
return;
if (page >= kNumberOfPages)
return;
// Replace the placeholder if necessary.
GraphController *controller = [self.graphControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null])
{
controller = [[GraphController alloc] initWithPageNumber:page data:[self.dataTripArray retain]];
[self.graphControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
}
// Add the controller's view to the scroll view.
if (controller.view.superview == nil)
{
CGRect frame = _scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[self.scrollView addSubview:controller.view];
}
}
I just removed the retain and the leak is no longer coming up. Fixed!

Dynamically created UIView Objects

I have a predicament, in as much that I need to create an arbitrary amount of UIView Objects. I have an NSArray and what I need to do is create UIView Objects for the number of items in the array, so I got an int from the [NSArray count]; method, so I know the number of objects needing creating, but the way to implement this has me stumped. I'll include some psudocode below to attempt to give across what I need to do:
[UIView returnMultipleUIViewsForInt:[theArray count]];
Obviously that won't work, but some way of creating an arbitrary amount of objects at runtime, which I can work with would be good.
So in short:
I need to create a certain number of UIViews based upon the number of items in an array.
I then need to access each view that is created and use it as a regularly created view might be used, doing things like adding one of them as a subview to a different view.
- (NSArray *)createNumberOfViews:(NSInteger)number
{
NSMutableArray *viewArray = [NSMutableArray array];
for(NSInteger i = 0; i < number; i++)
{
UIView *view = [[UIView alloc] init];
// any setup you want to do would go here, e.g.:
// view.backgroundColor = [UIColor blueColor];
[viewArray addObject:view];
[view release];
}
return viewArray;
}
NSMutableArray *newViews = [NSMutableArray array];
for (int i=0; i<[theArray count]; ++i) {
UIView *view = [[UIView alloc] init];
[newViews addObject:view];
[view release];
}

Resources