I'm not sure what I'm doing wrong. I'm thinking its because the NSMutableArray's *name and *field are declared in a different file.
#import <Foundation/Foundation.h>
#interface xmlToUrl : NSObject {
NSString *base_url;
NSMutableArray *field;
NSMutableArray *name;
}
#property(nonatomic,retain)NSString *base_url;
#property(nonatomic, retain)NSMutableArray *field;
#property(nonatomic, retain)NSMutableArray *name;
#end
and
#import "xmlToUrl.h"
#implementation xmlToUrl
#synthesize base_url;
#synthesize field;
#synthesize name;
-(id) init
{
self = [super init];
if (self) {
field = [[NSMutableArray alloc] init];
name = [[NSMutableArray alloc] init];
}
return self;
}
-(void)dealloc
{
[base_url release];
[field release];
[name release];
[super dealloc];
}
#end
The NSMutableArrays are filled in the ViewController class:
//nodeContent is just a NSMutableString being added to the *name and *field NSMutableArrays
if([ending isEqualToString:#"name"]){
if(![nodeContent isEqualToString:#""])
{[xmlToUrlObject.name addObject:nodeContent];}
}
else if([ending isEqualToString:#"field"]){
if(![nodeContent isEqualToString:#""])
{[xmlToUrlObject.field addObject:nodeContent];}
Error = Expected '.' before '.' token. (on the first line of the code below)
What I want for output: *url string contains base_url, urlPart2, and urlPart3 all joined in order.
ex:
base_url = #"www."
urlPart2 = #"mywebsite"
urlPart3 = #".com"
url = #"www.mywebsite.com"
//xmlToUrl is an instance of its own class (.m file) It contains the *name and *field NSMutableArray
//xmlToUrl.h is inherited in this file
NSMutableString *urlPart2 = [xmlToUrl.name objectAtIndex: 0];
[xmlToUrl.name removeObjectAtIndex:0];
NSMutableString *url = [xmlToUrl.base_url stringByAppendingString:urlPart2];
if([xmlToUrl.field count] != 0)
{NSMutableString *urlPart3 = [xmlToUrl.field objectAtIndex: 0];
[xmlToUrl.field removeObjectAtIndex:0}
url = [url stringByAppendingString:urlPart3];
[urlPart2 release];
[urlPart3 release];
You are trying to access a property on a class, and not on the instance:
#interface xmlToUrl : NSObject {
and
NSMutableString *urlPart2 = [xmlToUrl.name objectAtIndex: 0];
You can only access the property on an instance of xmlToUrl, just like you do in the following line of code:
{[xmlToUrlObject.name addObject:nodeContent];}
Here, xmlToUrlObject is an instance, and not the class.
Related
I want to retrieve a list of types from a "table" from my Main.xcdatamodeld (coredata) to put in a UIPickerView. This entity "Type" has only one attribute "name" (string).
If I put some strings in the NSMutableArray it works perfectly. The UIPickerView shows the strings. Above is the simple NSMutableArray.
resultFetch = [[NSMutableArray alloc]initWithObjects:#"Electronics",#"School",#"Kitchen",#"Office", nil];
But when I do a fetch from the coredata I get an error. Here's the code below.
AddItemViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
nameField.text = [self.currentItem name];
locationField.text = [self.currentItem location];
//typeField.text = [self.currentItem type];
quantityField.text = [[self.currentItem quantity] stringValue];
self.nameField.delegate = self;
self.locationField.delegate = self;
//self.typeField.delegate = self;
self.quantityField.delegate = self;
NSLog(#"ViewDidload before put fetch in array");
//Put fetch in NSArray resultFetch
AppDelegate *myApp = (AppDelegate *) [[UIApplication sharedApplication]delegate];
resultFetch = [[NSMutableArray alloc] init];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Type" inManagedObjectContext:[myApp managedObjectContext]];
[fetchRequest setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSError *error;
NSArray *fetchedObjects = [[myApp managedObjectContext] executeFetchRequest:fetchRequest error:&error];
for (Type *t in fetchedObjects) {
[resultFetch addObject:t];
NSLog(#"resultFetch: %#", resultFetch);
}
//Define typePicker
typePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0,43,320,480)];
typePicker.delegate = self;
typePicker.dataSource = self;
[typePicker setShowsSelectionIndicator:YES];
typeField.inputView = typePicker;
//Create done button in UIPickerView
myPickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 56)];
myPickerToolbar.barStyle = UIBarStyleBlackOpaque;
[myPickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(pickerDoneClicked)];
[barItems addObject:doneBtn];
[myPickerToolbar setItems:barItems animated:YES];
typeField.inputAccessoryView = myPickerToolbar;
NSLog(#"End ViewDidLoad");
}
-(void)pickerDoneClicked
{
NSLog(#"Done Clicked");
[typeField resignFirstResponder];
myPickerToolbar.hidden=YES;
typePicker.hidden=YES;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [resultFetch count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [resultFetch objectAtIndex:row];
}
- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
typeField.text = [resultFetch objectAtIndex:row];
}
The error is:
2013-11-14 10:31:53.099 Storage[1226:70b] ViewDidload before put fetch in array
2013-11-14 10:31:53.102 Storage[1226:70b] resultFetch: (
" (entity: Type; id: 0x8a83e50 ; data: )"
)
2013-11-14 10:31:53.103 Storage[1226:70b] resultFetch: (
" (entity: Type; id: 0x8a83e50 ; data: )",
" (entity: Type; id: 0x8a6ae80 ; data: )"
)
2013-11-14 10:31:53.104 Storage[1226:70b] resultFetch: (
" (entity: Type; id: 0x8a83e50 ; data: )",
" (entity: Type; id: 0x8a6ae80 ; data: )",
" (entity: Type; id: 0x8aa9650 ; data: )"
)
2013-11-14 10:31:53.107 Storage[1226:70b] End ViewDidLoad
2013-11-14 10:31:57.148 Storage[1226:70b] -[Type length]: unrecognized selector sent to instance 0x8aa08e0
It doesn't show the Type names that are inside the database.
Can you help me fetching this "table" properly to put those attribute strings inside a UIPickerView? All I want is a pretty NSMutableArray I can use! :)
Thank you very much for your help.
Paula
Problem solved! Changed the line inside for statement from [resultFetch addObject:t]; to [resultFetch addObject:t.name];.
Seems it was too much garbage! :)
I have a NSObject called GettingHere which has a NSString *content.
I then have a UIViewController on which I create a button programatically as follows (this button working as intended):
byAirButton = [UIButton buttonWithType:UIButtonTypeCustom];
byAirButton.tag = 1;
byAirButton.frame = CGRectMake(25, 140, 280.f, 40.f);
UIImage *airButton = [UIImage imageNamed:#"gettingHereByAirButton.png"];
[byAirButton setBackgroundImage:airButton forState:UIControlStateNormal];
[self.view addSubview:byAirButton];
[byAirButton addTarget:self action:#selector(byAirButtonClicked) forControlEvents:UIControlEventTouchUpInside];
For the action:#selector(byAirButtonClicked), I do the following. gettingHere is an instance of the GettingHere object.
- (void) byAirButtonClicked
{
gettingHere.content = #"This is how to get here by Air";
NSLog(#"Content: %#", gettingHere.content);
[self performSegueWithIdentifier:#"gettingHereSegue" sender:self];
}
The idea is to set the content for my GettingHere object and then just call that from the next view (GettingHereViewController) when the user clicks the byAirButton.
This NSLog shows that content is being set.
In my prepareForSegue, I do the following:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"gettingHereSegue"])
{
NSLog(#"Content to be passed: %#", gettingHere.content);
GettingHereViewController *vc = (GettingHereViewController *)segue.destinationViewController;
vc.gettingHere.content = gettingHere.content;
}
}
The segue works fine, but the NSLog shows my gettingHere object values as being (null).
Can anyone tell me where I am going wrong please? I have stepped through it several times but can't figure out where I am going wrong.
EDIT: Here is how I instantiate the GettingHere Object.
In the SubNavViewController.h
#import "GettingHereContent.h"
#interface SubNavViewController : UIViewController
#property GettingHereContent *gettingHere;
In the SubNavViewController.m
#import "SubNavViewController.h"
#import "GettingHereViewController.h"
#import "GettingHereContent.h"
#interface SubNavViewController ()
#end
#implementation SubNavViewController
#synthesize gettingHere;
And here is how I create the GettingHere Object:
GettingHere.h
#import <Foundation/Foundation.h>
#interface GettingHereContent : NSObject
#property (nonatomic, strong) NSString *content;
#end
GettingHere.m
#import "GettingHereContent.h"
#implementation GettingHereContent
#synthesize content;
#end
You never alloc init your gettingHere property. Try this in your init method of your VC
gettingHere = [[GettingHereContent alloc] init];
Also don't forget to release it: answer from here: alloc + init with synthesized property - does it cause retain count to increase by two?
#interface Foo : Bar {
SomeClass* bla;
}
#property (nonatomic, retain) SomeClass* bla;
#end
#implementation Foo
#synthesize bla;
-(id)init {
...
bla = [[SomeClass alloc] init];
...
}
-(void)dealloc {
[bla release];
...
[super dealloc];
}
Okay, so I'm pretty new to xcode but I cannot find something specific to this issue anywhere. So I declare the array and instantiate it in the following files:
//xmlToUrl.h
#import <Foundation/Foundation.h>
#interface xmlToUrl : NSObject {
NSMutableArray *field;
NSMutableArray *name;
}
#property(nonatomic, retain)NSMutableArray *field;
#property(nonatomic, retain)NSMutableArray *name;
#end
and
//xmlToUrl.m
#import "xmlToUrl.h"
#implementation xmlToUrl
#synthesize field;
#synthesize name;
-(void)dealloc
{
[field release];
[name release];
[super dealloc];
}
#end
So this is where I am confused. I don't know how to correctly "alloc" or "init" the mutable arrays, nor how to handle the add/remove operations from another file which inherits xmlToUrl.h.
The code (in the other file) as I have it now just prints null. Its listed below. What am I doing wrong?!?
//nodeContent is just a NSMutableString
[xmlToUrlObject.name addObject:nodeContent];
NSLog(#"xml Name = %#", xmlToUrlObject.name);
//I omitted all the operational code here but if I NSLog nodeContent it prints the correct values
[xmlToUrlObject.field addObject:nodeContent];
NSLog(#"xml Field = %#", xmlToUrlObject.field);
You need an init function in your implementation file, it would look similar to the following
-(id) init
{
self = [super init];
if (self) {
field = [[NSMutableArray alloc] init];
name = [[NSMutableArray alloc] init];
}
return self;
}
Then whenever you create an instance of your xmlToUrl class via [[xmlToUrl alloc] init] you will have an instance of your class that has already initialized your two NSMutableArrays
I have a UITableView populated by data parsed from xml. The parsing works but the table remains blank.
The console shows that the xml form the url is parsed and shows its components. It also shows the number of objects that the rows of tableview should have when asked in a different function but the numberOfRowsInSection: is returning Null. Therefore, the tableView in the Simulator remains blank.
Here is my code. It is simple code from a tutorial:
+++++++++++++++++ RootViewController.h++++++++++++++++++++++
#import < UIKit/UIKit.h >
#interface RootViewController : UITableViewController < NSXMLParserDelegate >{
IBOutlet UITableView *newsTable;
UIActivityIndicatorView *activityIndicator;
CGSize cellSize;
NSXMLParser *rssParser;
NSMutableArray *stories;
NSMutableDictionary *item;
NSString *currentElement;
NSMutableString *currentTitle, *currentDate, *currentSummary, *currentLink;
}
#property (nonatomic, retain) NSMutableArray *stories;
#property (nonatomic, retain) IBOutlet UITableView *newsTable;
-(void)parseXMLFileAtURL:(NSString *)URL;
#end
+++++++++++++++++++++++++++ RootViewController.m ++++++++++++++++++++++++++++++++++++++
#import "RootViewController.h"
#implementation RootViewController
#synthesize newsTable, stories;
-(void)viewDidLoad {
[super viewDidLoad];
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[newsTable reloadData];
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if([stories count] == 0){
NSString *path = #"http://feeds.feedburner.com/TheAppleBlog";
[self parseXMLFileAtURL:path];
}
cellSize = CGSizeMake([newsTable bounds].size.width, 60);
}
// Customize the number of sections in the table view.
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(void)parseXMLFileAtURL:(NSString *)URL {
stories = [[NSMutableArray alloc] init];
NSURL *xmlURL = [NSURL URLWithString:URL];
rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
[rssParser setDelegate:self];
[rssParser setShouldProcessNamespaces:NO];
[rssParser setShouldReportNamespacePrefixes:NO];
[rssParser setShouldResolveExternalEntities:NO];
[rssParser parse];
}
-(void)parserDidStartDocument:(NSXMLParser *)parser{
NSLog(#"Found file and started parsing");
}
-(void) parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError{
NSString *errorString = [NSString stringWithFormat:#"Unable to download story feed from the website (error code %i)", [parseError code]];
NSLog(#"error parsing XML: %#", errorString);
UIAlertView *errorAlert = [[UIAlertView alloc]:#"Error loading content" message:errorString delegate:self cancelButtonTitle:#"OK" otherButtonTitle:nil];
[errorAlert show];
}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
NSLog(#"Found this Element %#", elementName);
currentElement = [elementName copy];
if ([elementName isEqualToString:#"item"]) {
item = [[NSMutableDictionary alloc] init];
currentTitle = [[NSMutableString alloc] init];
currentDate = [[NSMutableString alloc] init];
currentSummary = [[NSMutableString alloc] init];
currentLink = [[NSMutableString alloc] init];
}
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
NSLog(#"End this Element %#", elementName);
if ([elementName isEqualToString:#"item"]) {
[item setObject:currentTitle forKey:#"title"];
[item setObject:currentLink forKey:#"link"];
[item setObject:currentSummary forKey:#"summary"];
[item setObject:currentDate forKey:#"date"];
[stories addObject:[item copy]];
NSLog(#"adding Story : %#",currentTitle);
}
}
// Customize the number of rows in the table view.
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(#"Count is = %#", [stories count]);
return [stories count];
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
NSLog(#"Found characters: %#", string);
if([currentElement isEqualToString:#"title"]){
[currentTitle appendString:string];
NSLog(#"The Title is : %#", currentTitle);
}
else if([currentElement isEqualToString:#"link"]){
[currentLink appendString:string];
NSLog(#"The Link is : %#", currentLink);
}
else if([currentElement isEqualToString:#"description"]){
[currentSummary appendString:string];
NSLog(#"The summary is : %#", currentSummary);
}
else if([currentElement isEqualToString:#"pubDate"]){
[currentDate appendString:string];
NSLog(#"The Date is : %#", currentDate);
}
}
-(void)parserDidEndDocument:(NSXMLParser *)parser{
[activityIndicator stopAnimating];
[activityIndicator removeFromSuperview];
NSLog(#"Stories array has %d items", [stories count]);
NSLog(#"Stories are : %#",stories);
}
// Customize the appearance of table view cells.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
}
// Configure the cell.
cell.textLabel.text = (NSString *)[[stories objectAtIndex:indexPath.row] objectForKey:#"title"];
return cell;
}
-(void)dealloc {
[super dealloc];
[newsTable release];
[currentDate release];
[currentElement release];
[currentSummary release];
[currentLink release];
[stories release];
[item release];
[currentTitle release];
[rssParser release];
}
You have to tell the table view that there is new data by calling reloadData after you have parsed the XML.
Is the newsTable outlet properly connected to the table view in IB? And, is the table's dataSource outlet set to your view controller?
To expand on #omz's correct answer:
The method numberOfRowsInSection is not returning NULL but zero. (In Objective-C, nil==zero and Null is a singleton object.)
The only reason that it would return zero is if the [stories count] returns zero and the only reason [stories count] would return zero is if it has no elements. Since you've confirmed that the parse works and stories has elements, then the tableview must be seeking data before the parse occurs.
This method is called first and it is the only place you reload data:
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[newsTable reloadData];
// You trigger the tableview to call numberOfRowsInSection before stories is populated.
}
This method is called only after the tableview has appeared on screen and it is only after the tableview appears that you populate stories.
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if([stories count] == 0){
NSString *path = #"http://feeds.feedburner.com/TheAppleBlog";
[self parseXMLFileAtURL:path];
}
cellSize = CGSizeMake([newsTable bounds].size.width, 60);
}
However, nothing triggers the tableview to call numberOfRowsInSection again so the tableview remains blank. Simply moving the populating of stories to viewWillAppear: will fix the problem.
Each time you alter the data upon which the tableview depends (regardless of reason) you must call reloadData otherwise the tableview remains unaware that it no longer displays the current data set.
As an aside, you should use the dot notation when referring to properties to ensure they are properly retained. You should use self.stories to refer to the stories property. Otherwise, it might be released at random causing an equally random crash.
I read some materials about this then I tried to wrote a simple
app but got confused, here is the sample (using cocos2d template):
MySpriteObject class:
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#interface MySpriteObject : CCNode {
CCSprite *mySprite;
NSString *spriteInfo;
}
#property(nonatomic,retain) CCSprite *mySprite;
#property(nonatomic,retain) NSString *spriteInfo;
#end
#import "MySpriteObject.h"
#implementation MySpriteObject
#synthesize mySprite;
#synthesize spriteInfo;
-(id) init
{
NSLog(#"MySpriteObject init");
if ((self = [super init])) {
self.mySprite = [CCSprite spriteWithFile:#"Icon.png"];
self.spriteInfo = [[[NSString alloc] initWithFormat:#"sprite info"] autorelease];
}
return (self);
}
-(void)dealloc
{
NSLog(#"MySpriteObject dealloc");
[self.spriteInfo release];
[super dealloc];
}
#end
MyObjectManager class:
#import "cocos2d.h"
#import "MySpriteObject.h"
#class MySpriteObject;
#interface MyObjectManager : CCNode {
}
+(MyObjectManager *)sharedMyObjectManager;
-(NSMutableArray *)func1;
-(NSMutableArray *)func2;
#end
#import "MyObjectManager.h"
#import "MySpriteObject.h"
#implementation MyObjectManager
static MyObjectManager *_sharedMyObjectManager = nil;
+(MyObjectManager *)sharedMyObjectManager
{
NSLog(#"MyObjectManager sharedMyObjectManager");
if (!_sharedMyObjectManager) {
if( [ [MyObjectManager class] isEqual:[self class]] )
_sharedMyObjectManager = [[MyObjectManager alloc] init];
else
_sharedMyObjectManager = [[self alloc] init];
}
return _sharedMyObjectManager;
}
-(id)init
{
NSLog(#"MyObjectManager init");
if( (self = [super init]) ) {
}
return self;
}
-(void)dealloc
{
NSLog(#"MyObjectManager dealloc");
[super dealloc];
}
-(NSMutableArray *)func1
{
NSMutableArray *array1 = [[NSMutableArray alloc] init];
array1 = [self func2];
return array1;
}
-(NSMutableArray *)func2;
{
NSMutableArray *array2 = [[NSMutableArray alloc] init];
for (int i = 0; i < 3; i++) {
MySpriteObject *mySpriteObject = [[MySpriteObject alloc] init];
[array2 addObject:mySpriteObject];
//[mySpriteObject release];
}
return array2;
}
#end
And in the "HelloWorldScene.m" init method:
-(id) init
{
if( (self=[super init] )) {
NSMutableArray *array = [[NSMutableArray alloc] init];
array = [[MyObjectManager sharedMyObjectManager] func1];
for (int i = 0; i < array.count; i++) {
MySpriteObject *s = [[MySpriteObject alloc] init];
s = [array objectAtIndex:i];
NSLog(#"%#", s.spriteInfo);
[s release];
}
[array release];
}
return self;
}
The code posted above works fine (at least no crash), but I think there is
memory leak out there.
At first, I had the fun1 fun2 method return the array like this:
return [array1 autorelease];
return [array2 autorelease];
And released the SpriteObject in the func2 methoed as you can see the commented line.
But the app crashed.
Then I uncommented the SpriteObject release line but it still crashed.
Then I deleted the two autorelease in the return statement and it worked
fine.
Can somebody give something suggestion according to the code above???
Thanks in advance.
Oh boy, there are multiple problems. At first remember to release everything you called "alloc, new, retain, copy" upon. See Objective C release, autorelease, and data types and Release, Dealloc, and the Self reference
The basic problem here is, that you are overwriting previously created variables. For example:
NSMutableArray *array = [[NSMutableArray alloc] init];
array = ... ;
[array release];
Therefore you create a memory leak and get double release trouble. See obj-c NSString and alloc / retain / release for how to do it right.