Iphone variable is loading syntax it seems and not the variable - iphone

I'm assigning a variable string from an XML node and for some reason when i'm using it later i'm getting it read like this:
edit
The Current URL to parse is a NSLog that is written when the page is loaded from the [loadXML currentCat]]
NSLog:
Current URL To Parse: <UILongPressGestureRecognizer: 0x677c240; state = Possible; view = <UITableViewCellContentView 0x67780b0>; target= <(action=_longPressGestureRecognized:, target=<UITableViewCell 0x677c340>)
tempFullUrl being set - this is in my class.h
#interface PromotionViewController : UITableViewController {
NSString *tempFullUrl;
}
#property (nonatomic, retain) NSString *tempFullUrl;
#end
It's also synthasised in the .m
Var being Set:
NSArray *urlArray = [item elementsForName:kName_url];
for(CXMLElement *url in urlArray)
{
newobj.urlSearch = url.stringValue;
tempFullUrl = url.stringValue;
NSLog(#"Temp Full URL: : %#", tempFullUrl);
[url release];
[kName_url release];
}
[totalArray addObject:newobj];
}}
Var being used:
else if(currentType == #"p2FullSearch") {
NSString *searchType = #"categorySearch";
PromotionViewController *loadXML = [[PromotionViewController alloc] initWithNibName:#"PromotionViewController" bundle:nil];
[loadXML setCurrentCat: tempFullUrl];
[loadXML setCurrentType: searchType];
[self.navigationController pushViewController:loadXML animated:YES];
}
The app falls over, highlighting the tempFullUrl = url.stringValue; line but doesn't say why! I assume as the var tempFullUrl is being set weirdly
Any help would be appreciated
Tom

You're not going through your synthesised accessor, so the string is getting released. Use
self.tempFullURL =
Instead of assigning to the ivar directly. In your case it looks like the area of memory has been reassigned to a gesture recogniser which is showing up in your log statements.

Related

Getting the id value corresponding to the selectedsegment for segmentControl iphone

First of all sorry for the title.
I have one segment control in my app.The titles of the segments are fetching from the server and setting accordingly(ie YES and NO).
There is also one corresponding id value for each segment.So when the user select YES a corresponding id 8 is to saved,and when user Select NO a corresponding id 5 is to be saved(8 and 5 are coming from the server).
I tried by setting the tags but in vain.
Can anyone please help me with this scenario.
Thanks a lot in advance.
//Code
for(WTMobileDataService_DataAccess_ClsYesNo *temp in YesNo)
{
NSString *s = [NSString stringWithFormat:#"%#",temp.m_YesNoDescription];
ind = [temp.m_PK_YesNoID intValue] - 1;
[items addObject:s];
[serverids addObject:[NSString stringWithFormat:#"%d",ind]];
// [segmentControl setTitle:s forSegmentAtIndex:ind];
}
segmentControl = [[UISegmentedControl alloc]initWithItems:items];
Since each UISegmentedControl has only one tag, you'll need to do something like this:
NSMutableArray *items = [NSMutableArray new];
NSMutableArray *serverIds = [NSMutableArray new];
for (ServerResponseObject *response in serverResponses) {
[items addObject:response.title];
[serverIds addObject:response.id];
}
mySegmentedControl = [[UISegmentedControl alloc] initWithItems:items];
Thereafter, when the user taps on the UISegmentedControl, access the selectedIndex property of the segmented control to find out which segment they tapped on, and use this value as the key to the serverIds array to see what the corresponding ID number is according to the server.
Have two arrays, one with the titles, the other with the values. When the user selects a segment, use the selected segment to return the title, and then use that to get the index of the title. Then you can reference the values array and you've got the corresponding value.
You could also achieve the same thing using an NSDictionary where the title is your key, and the value is... the value.
Something like this:
NSArray titles = #[#"YES", #"NO"];
NSArray values = #[8, 5];
UISegmentedControl control; // Likely defined as a property
if ([control selectedSegmentIndex] != UISegmentedControlNoSegment) {
NSString title = [control titleForSegmentAtIndex:];
int index = [titles indexOfObject:title];
NSString value = [values objectAtIndex:index];
}
I would be tempted to create an object to hold the two values together as they seem like a natural grouping
#interface SegmentInfo : NSObject
#property (nonatomic, copy) NSString *title;
#property (nonatomic, assign NSInteger value;
- (instancetype)initWithTitle:(NSString *)title value:(NSInteger)value;
#end
#implementation SegmentInfo
- (instancetype)initWithTitle:(NSString *)title value:(NSInteger)value
{
self = [super init];
if (self) {
_title = [title copy];
_value = value;
}
return self;
}
#end
Now you populate these objects and store them in an array
self.segmentInfos = #[
[[SegmentInfo alloc] initWithTitle:#"Yes" value:8],
[[SegmentInfo alloc] initWithTitle:#"No" value:5],
];
To set up the segmented control you just grab all the titles into an array
some set up method
{
NSArray *segmentTitle = [self.segmentInfo valueForKey:#"title"];
self.segmentControl = [[UISegmentedControl alloc] initWithItems:segmentTitle];
}
Now in your method that handles value changes you simple grab the object at the same index and get the value
- (IBAction)segmentedControlChanged:(UISegmentedControl *)segmentedControl
{
SegmentInfo *selectedSegment = self.segmentInfos[segmentedControl.selectedSegmentIndex];
NSLog(#"title: %#, value: %d", selectedSegment.title, selectedSegment.value);
}

how to pass a string value from one .m file to another .m file

Hi i am new to objective c. i have a .m file in which i have the string to be passed.
NSString *passedMonth;
i am Passing it like this
KLTile *kltil = [[KLTile alloc] inittempMonth:passedMonth];
temp month is string in other .m file
-(id)inittempMonth:(NSString *)tem{
tempMonth = [[NSString alloc]init];
self.tempMonth = tem;
NSLog(#" temp month....%#",self.tempMonth);
return self;
}
the log inside the init is giving the output but the same log outside the init method doe't works......
i want to use the tempMonth string outside the -(id)inittempMonth:(NSString *)tem{...
is there any way to use the string outside the init method...
Your init method is wrong. So you should modify it into something like:
- (id)initWithTempMonth:(NSString *)tem{
{
self = [super init]; // This line is important
if (self) {
self.tempMonth = tem;
NSLog(#" temp month....%#",self.tempMonth);
}
return self;
}
Also, don't forget to declare tempMonth as a retained property in the .h file:
#property (nonatomic, retain) NSString *tempMonth;
Or if you are using ARC:
#property (nonatomic, strong) NSString *tempMonth;
Then you can log the value of the property like this:
KLTile *kltil = [[KLTile alloc] inittempMonth:passedMonth];
NSLog(#"Temp month: %#", kltil.tempMonth);

problem with NSString, it's losing it's value after assigning it to a function parameter

Objective-C is really wierd, i can't get the hang of it...
I have a NSstring that is losing it's value if I try to reassign it...
Here's how I use it..
Can anyone tell me what am I doing wrong? it's happening at the assigning of the new value..
#interface PageViewController : UIViewController {
NSString *mystring;
}
- (void)viewDidLoad {
mystring=[ [NSString alloc] initWithString:#""];
}
-(void) function_definition:(NSString *) param {
.............
mystring=param;
.........
}
Most commonly, you would want to designate this as a property:
#interface PageViewController : UIViewController {
NSString *mystring;
}
#property (nonatomic, retain) NSString *mystring;
Then in your implementation,
#synthesize mystring;
- (void)dealloc {
[mystring release];
[super dealloc];
}
And finally, anywhere in your implementation, set the value of mystring by using either:
[self setMystring:#"something"];
or
self.mystring = #"somethingelse";
If you're allocating a new string, be sure to release it. It's retained automatically using the property.
self.mystring = [[[NSString alloc] initWithString:#"hello"] autorelease];
Lastly, in your function:
-(void) function_definition:(NSString *) param {
.............
self.mystring = param;
.........
}
It's not completely clear what you mean by 'losing its value', but I think the problem here is one of memory management- you need to do some reading of how Cocoa handles this, but in this case you'll need to do:
-(void) function_definition:(NSString *) param {
.............
if (mystring != param) {
[mystring release];
mystring = [param retain];
}
.........
}

passing NSString from one class to the other

I have a NSString that is taken from a UITextField in a ViewController. Every of my other ViewController will use this NSString as well. How can I pass this NSString to others ViewControllers?
You want to have a property in each of your controllers
#interface MyViewController : UIViewController{
NSString *title;
}
#property (retain) NSString *title;
#end;
#implementation MyViewController
#synthesize title;
#end;
Use it like:
MyViewController *myVC = [[MyViewController alloc] initWithFrame:...];
myVC.title = #"hello world";
You should be familiar with Memory Management
Create a class for sharing your common objects. Retrieve it using a static method, then read and write to its properties.
#interface Store : NSObject {
NSString* myString;
}
#property (nonatomic, retain) NSString* myString;
+ (Store *) sharedStore;
#end
and
#implementation Store
#synthesize myString;
static Store *sharedStore = nil;
// Store* myStore = [Store sharedStore];
+ (Store *) sharedStore {
#synchronized(self){
if (sharedStore == nil){
sharedStore = [[self alloc] init];
}
}
return sharedStore;
}
// your init method if you need one
#end
in other words, write:
Store* myStore = [Store sharedStore];
myStore.myString = #"myValue";
and read (in another view controller):
Store* myStore = [Store sharedStore];
myTextField.text = myStore.myString;
If the string remains the same, and never changes, you could make a file named defines.h (without the .m file) and have this line:
#define kMyString #"Some text"
Then wherever you need the string, just import the defines file and use the constant.
#import "defines.h"
Much simpler than making custom classes.
EDIT:
Didn't see you needed to grab from the text field.
In that case, you could have it stored as property of your app delegate class and get it from there. The delegate can be accessed from anywhere in your app.

iPhone SDK: NSMutableArray count causes EXC_BAD_ACCESS

This is really twisting my mind… I'm trying to access an NSMutableArray in an IBAction which I defined in viewDidLoad. Unfortunately I keep getting a EXC_BAD_ACCESS.
I'm new to all this so I'd really appreciate some insight in what I'm doing wrong.
Below find the corresponding code excerpts.
CounterViewController.h:
#interface CounterViewController : UIViewController{
NSMutableArray *countHistoryArray;
}
#property(nonatomic, retain) NSMutableArray *countHistoryArray;
CounterViewController.m:
#implementation CounterViewController
#synthesize countHistoryArray;
- (void)viewDidLoad {
[super viewDidLoad];
//Fill array with some dummy data
self.countHistoryArray = [[NSMutableArray alloc] init];
NSDate *now = [[[NSDate alloc] init] autorelease];
CurrentCount *historicCount = [[[CurrentCount alloc]
initWithCount:[NSNumber numberWithInteger:22]
description:#"Testcount"
dateAndTime:now] autorelease];
[self.countHistoryArray addObject: historicCount];
//Do some logging - everything is working fine here!
NSLog(#"%#", [self.countHistoryArray description]);
}
//Later on we click on a button and want to use the array
- (IBAction)doSomeStuff {
//Let's look at the array again - and now it crashes with EXC_BAD_ACCESS
NSLog(#"%#", [self.countHistoryArray description]);
}
Thanks a lot!
Manuel
EDIT Additional code as asked for by #jamapag
CurrentCount.h
#import <Foundation/Foundation.h>
#interface CurrentCount : NSObject {
NSNumber *counterLevel;
NSString *description;
NSDate *dateAndTime;
}
- (id)initWithCount:(NSNumber *)newCounterLevel description:(NSString *)newDescription dateAndTime:(NSDate *)newDateAndTime;
#property(nonatomic, copy) NSNumber *counterLevel;
#property(nonatomic, copy) NSString *description;
#property(nonatomic, copy) NSDate *dateAndTime;
#end
CurrentCount.m
#import "CurrentCount.h"
#implementation CurrentCount
#synthesize counterLevel;
#synthesize description;
#synthesize dateAndTime;
- (id)initWithCount:(NSNumber *)newCounterLevel description:(NSString *)newDescription dateAndTime:(NSDate *)newDateAndTime{
self = [super init];
if(nil != self){
self.counterLevel = newCounterLevel;
self.description = newDescription;
self.dateAndTime = newDateAndTime;
}
return self;
}
-(void) dealloc{
self.counterLevel = nil;
self.description = nil;
self.dateAndTime = nil;
[super dealloc];
}
#end
Are you sure that your code actually looks like this?
- (IBAction)doSomeStuff {
//Let's look at the array again - and now it crashes with EXC_BAD_ACCESS
NSLog(#"%#", [self.countHistoryArray description]);
}
Your question title says "NSMutableArray count causes EXC_BAD_ACCESS" - if that line of code actually says NSLog(#"%#", [self.countHistoryArray count]);, you'll almost certainly get a crash, since NSLog will attempt to treat a primitive type (the type returned by -[NSArray count]) as an object. In order to use -[NSArray count] in NSLog, use %u instead of %#:
- (IBAction)doSomeStuff {
// This time it should work!
NSLog(#"Array Count = %u", [self.countHistoryArray count]);
}
Remove autorelease from:
currentCount *historicCount = [[[CurrentCount alloc]
initWithCount:[NSNumber numberWithInteger:22]
description:#"Testcount"
dateAndTime:now] autorelease];
It looks like you are accidentally releasing countHistoryArray somewhere. Try removing all calls to it except for those two you showed. Additionally you can try enabling zombies to debug the problem.
Oh and by the way you probably don't really want a public NSMutableArray property and if you do you probably want it to be copy, not retain. Otherwise incapsulation kinda goes down the drain.
I know this question has already been solved and accepted but its for others who are or will face this issue.
I was facing the same issue, I tried all solutions but no solution worked for me. The project I am working on is NON-ARC.
I tried and made a simple change in property
Previously my property for NSMUTABLEARRAY was
#property (nonatomic, assign) NSMutableArray * dataArray;
I changed it to:
#property (nonatomic, retain) NSMutableArray * dataArray;
Changed it from ASSIGN to RETAIN
And it solved my problem