Accessing NSString between methods causes app crash - iphone

I apologise if this is a bad question as I'm new to iOS development. So here is my problem: I declared a class variable NSString, the string is assigned a string value from a textView but when i try to access the string from other method, the app crashes. Here's the code:
Interface: (ClassName.h)
#interface ClassName: UIViewController <UITextViewDelegate>{}
#property (nonatomic, assign)NSString *strSomeText;
#end
Implementation: (ClassName.m)
#implementation
#synthesize strSomeText;
- (void)textViewDidChange:(UITextView *)textView{
strSomeText = textView.text;
NSLog(#"%#", strSomeText); //This line works just fine
}
- (void)textViewDidEndEditing:(UITextView *)textView{
NSLog(#"%#", strSomeText); //this line causes the app to crash
}
#end
Thanks for the help!
Loc.

Your problem is likely due to the fact that you're using assign for your property. This will mean that the string can be deallocated while you still have a reference to it. Try using copy instead:
#property (nonatomic, copy) NSString *strSomeText;
Then you should use your property accessor in your textViewDidChange: method:
self.strSomeText = textView.text;

Related

I'm trying to pass a string from my first ViewController to my second ViewController but it returns NULL

In my first view controller I have 3 input fields each of them take the user input into and saves it into a string such as: address, username and password as NSUserDefaults. This part works fine.
In my second view controller I'm trying to take the 3 strings from first controller (address, username and password) create a html link based on the 3 strings. I've tried many ways to access the 3 strings with no luck, the result I get is NULL.
Here is my code:
//.h file - first view controller with the 3 input fields CamSetup.h
#import <UIKit/UIKit.h>
#interface CamSetup : UIViewController <UITextFieldDelegate>
{
NSString * address;
NSString * username;
NSString * password;
IBOutlet UITextField * addressField;
IBOutlet UITextField * usernameField;
IBOutlet UITextField * passwordField;
}
-(IBAction) saveAddress: (id) sender;
-(IBAction) saveUsername: (id) sender;
-(IBAction) savePassword: (id) sender;
#property(nonatomic, retain) UITextField *addressField;
#property(nonatomic, retain) UITextField *usernameField;
#property(nonatomic, retain) UITextField *passwordField;
#property(nonatomic, retain) NSString *address;
#property(nonatomic, retain) NSString *username;
#property(nonatomic, retain) NSString *password;
#end
//.m file - first view controller CamSetup.m
#import "CamSetup.h"
#interface CamSetup ()
#end
#implementation CamSetup
#synthesize addressField, usernameField, passwordField, address, username, password;
-(IBAction) saveAddress: (id) sender
{
address = [[NSString alloc] initWithFormat:addressField.text];
[addressField setText:address];
NSUserDefaults *stringDefaultAddress = [NSUserDefaults standardUserDefaults];
[stringDefaultAddress setObject:address forKey:#"stringKey1"];
NSLog(#"String [%#]", address);
}
-(IBAction) saveUsername: (id) sender
{
username = [[NSString alloc] initWithFormat:usernameField.text];
[usernameField setText:username];
NSUserDefaults *stringDefaultUsername = [NSUserDefaults standardUserDefaults];
[stringDefaultUsername setObject:username forKey:#"stringKey2"];
NSLog(#"String [%#]", username);
}
-(IBAction) savePassword: (id) sender
{
password = [[NSString alloc] initWithFormat:passwordField.text];
[passwordField setText:password];
NSUserDefaults *stringDefaultPassword = [NSUserDefaults standardUserDefaults];
[stringDefaultPassword setObject:password forKey:#"stringKey3"];
NSLog(#"String [%#]", password);
}
- (void)viewDidLoad
{
[addressField setText:[[NSUserDefaults standardUserDefaults] objectForKey:#"stringKey1"]];
[usernameField setText:[[NSUserDefaults standardUserDefaults] objectForKey:#"stringKey2"]];
[passwordField setText:[[NSUserDefaults standardUserDefaults] objectForKey:#"stringKey3"]];
[super viewDidLoad];
}
#end
//.h second view controller LiveView.h
#import <UIKit/UIKit.h>
#import "CamSetup.h"
#interface LiveView : UIViewController
{
NSString *theAddress;
NSString *theUsername;
NSString *thePassword;
CamSetup *camsetup; //here is an instance of the first class
}
#property (nonatomic, retain) NSString *theAddress;
#property (nonatomic, retain) NSString *theUsername;
#property (nonatomic, retain) NSString *thePassword;
#end
//.m second view LiveView.m file
#import "LiveView.h"
#interface LiveView ()
#end
#implementation LiveView
#synthesize theAddress, theUsername, thePassword;
- (void)viewDidLoad
{
[super viewDidLoad];
theUsername = camsetup.username; //this is probably not right?
NSLog(#"String [%#]", theUsername); //resut here is NULL
NSLog(#"String [%#]", camsetup.username); //and here NULL as well
}
#end
There are 5 issues in this code:
If you are using ARC, all the "retain" in your #properties should be changed to "strong"
You name your iVars and properties the same thing. (common bad practice)
You are always directly accessing iVars and not properties in your code.
You don't retain your instance of CamSetup in the second object.
The direct cause of your problem: in the second object you've only created a placeholder for a CamSetup instance, you've not created one nor passed one to it! self.camSetup in your second object is empty right now.
Let's go step by step:
First, give your iVars different names from your properties. This is best practice, especially for a beginner! :)
#interface CamSetup : UIViewController <UITextFieldDelegate>
{
NSString *_address;
}
#property(nonatomic, strong) NSString *address;
#end
#implementation CamSetup
#synthesize address=_address;
...
#end
This is important, because you've setup properties, but in your code, you are not using them, you are directly accessing your iVars. Since you've named them the same thing, you might not see this.
Let's look at your first object. Every "address" in your code is going to your iVar and not property. Generally, you want to access the iVars via your properties unless you're sure otherwise. The #synthesize creates a getter and setter method for your iVar that will retain the var because you told it to in your #property statement. However, when you directly access your iVar's you're not going through those accessors and thus the stuff you wrote in your #properties doesn't matter. You could end up misunderstanding a lot of errors and bugs if you aren't clear about this. In your first object this worked anyway because the alloc/init sets a retain on the object, but I noticed you always do this, and that's going to get you into trouble.
Here's what the saveAddress: method would look like using properties:
-(IBAction) saveAddress: (id) sender
{
self.address = [[NSString alloc] initWithFormat:self.addressField.text];
[self.addressField setText:self.address];
NSUserDefaults *stringDefaultAddress = [NSUserDefaults standardUserDefaults];
[stringDefaultAddress setObject:self.address forKey:#"stringKey1"];
NSLog(#"String [%#]", self.address);
}
Next, in your second object you need to set properties for the CamSetup instance! Right now, you just have an iVar.
#import <UIKit/UIKit.h>
#import "CamSetup.h"
#interface LiveView : UIViewController
{
NSString *_theAddress;
NSString *_theUsername;
NSString *_thePassword;
CamSetup *_camSetup; //here is an instance of the first class
}
#property (nonatomic, retain) CamSetup *camSetup; // in synthesize we'll specify that this property uses the _camSetup iVar
#property (nonatomic, strong) NSString *theAddress;
#property (nonatomic, strong) NSString *theUsername;
#property (nonatomic, strong) NSString *thePassword;
#end
The implementation:
#import "LiveView.h"
#interface LiveView ()
#end
#implementation LiveView
#synthesize camSetup = _camSetup;
#synthesize theAddress = _theAddress;
#synthesize theUsername = _theUsername;
#synthesize thePassword = _thePassword;
- (void)viewDidLoad
{
[super viewDidLoad];
self.theUsername = self.camSetup.username; //this is probably not right?
// well, it's better now, but where is camSetup coming from??? it's undefined now
NSLog(#"String [%#]", self.theUsername); //resut here is NULL
NSLog(#"String [%#]", self.camSetup.username); //and here NULL as well
}
#end
We've created an iVar and property pair that will hold a pointer to a CamSetup instance and will retain that pointer (if we set it using the property). However, where is that CamSetup instance being created? Where do you alloc/init it?
There are many possible answers to this question. If CamSetup had getters for address, username, password that read them back in from your user defaults, then all you'd have to do is alloc/init a CamSetup and set it to camSetup. However, right now your first object has no functionality to retrieve the saved data so we can't do that. (still, this is the solution I'd hope you'd implement).
You might be initializing both of these in your app delegate? However, if you are using storyboard then likely it is initializing these object for you when it initializes your interface. In this case, in your appDelegate app has finished launching... method, you'll have to retrieve pointers to these instances, then the camSetup property on your second object, to point to the first. To tell you how to do this, we'd have to know detailed specifics of your app. Still, this wouldn't be doing it the best way.
Best practice would be to create an object which saves and retrieves these data from user defaults for you. This future proofs your implementation should you later want to change the way you store these data. You'd just change the way they are stored/retrieved within their class.
Your problem is here:
CamSetup *camsetup; //here is an instance of the first class
You aren't doing anything to make camsetup refer to the same instance of the CamSetup class that is taking input from the user. The camsetup variable is never initialized, hence it's NULL, hence any properties you try to retrieve from it will also be NULL.
How exactly you'd fix this depends on the structure of your program. One possibility is to have whatever code is creating the LiveView controller set the camsetup value. For example:
LiveView *liveViewController = [LiveView alloc] initWithNibName:#"LiveView" bundle:nil]];
liveViewController.camsetup = camSetupController;
(you'd need to make camsetup a property to do this).
BUT, from a design standpoint, having the one view controller have a reference to the other is probably the wrong way to go about solving this problem -- it introduces unnecessary dependencies between the two controllers. For example, say you later decide to make it possible to go directly to the LiveView controller upon program launch, using a saved name/password; you can't do that if LiveView depends on getting its input from a CamSetup object.
So, a better approach might be to have LiveView take its input from NSUserDefaults, and/or by having the code that's calling LiveView set the username/password properties.
-- edit --
For example, retrieve the data from the user defaults like:
self.address = [[NSUserDefaults standardUserDefaults] objectForKey:#"stringKey1"];
Is the camsetup initialized? If not -- initialize it first.
There are many ways to do this, most of which depend on how the two controllers are related to each other (like how are you going from the first to the second).
However, in your case you don't need to pass the string at all, since you are saving them in user defaults. Just read the values of those defaults in your second controller.
Hi i got your problem just do one thing.
When you are setting value in NSUserDefaults it need synchronization so just synchronize it.
[stringDefaultUsername synchronize];
And retrieve data using
NSString *username = [[NSUserDefaults standardUserDefaults] objectForKey:#"stringKey2"];
Follow apple NSUserDefaults Class Reference for more info

Iphone Text Array, Error when passing string to method. Can't explain, must be array terminator ?

I am getting NSCFString Errors passing the results of a text string converted into an array to a method that expects strings.
I have a feeling that the problem is that there is something wrong with the array conversion but i am not clever enough to work this out !!!
here's the .h file
#interface RootViewController : UIViewController <ZXingDelegate> {
IBOutlet UITextView *resultsView;
NSString *resultsToDisplay;
IBOutlet UITextField *ItemNo;
NSString *ItemNoToDisplay;
IBOutlet UITextField *VariantCode;
NSString *VariantCodeToDisplay;
IBOutlet UITextField *Description;
NSString *DescriptionToDisplay;
IBOutlet UITextField *Qty;
NSString *QtyToDisplay;
}
#property (nonatomic, retain) IBOutlet UITextView *resultsView;
#property (nonatomic, copy) NSString *resultsToDisplay;
#property (nonatomic, retain) IBOutlet UITextField *ItemNo;
#property (nonatomic,copy) NSString *ItemNoToDisplay;
#property (nonatomic, retain) IBOutlet UITextField *VariantCode;
#property (nonatomic,copy) NSString *VariantCodeToDisplay;
#property (nonatomic, retain) IBOutlet UITextField *Description;
#property (nonatomic,copy) NSString *DescriptionToDisplay;
#property (nonatomic, retain) IBOutlet UITextField *Qty;
#property (nonatomic,copy) NSString *QtyToDisplay;
in the .m file i am doing this, the code is based on the Zxing Barcode scanning scan test application.
The barcode i am scanning has a string separated by ;
- (void)zxingController:(ZXingWidgetController*)controller didScanResult:(NSString *)result {
self.resultsToDisplay = result;
if (self.isViewLoaded)
{
//This is where the result comes back from the scanner.
//Need to use this to add items to a basket etc
//This is where we can create a new basket screen...
//NSString *myString = #"This is a test";
NSArray *myArray = [result componentsSeparatedByString:#";"];
ItemNoToDisplay = [myArray objectAtIndex:0];
[ItemNo setText:ItemNoToDisplay];
[ItemNo setNeedsDisplay];
VariantCodeToDisplay = [myArray objectAtIndex:1];
[VariantCode setText:VariantCodeToDisplay];
[VariantCode setNeedsDisplay];
DescriptionToDisplay = [myArray objectAtIndex:2];
[Description setText:DescriptionToDisplay];
[Description setNeedsDisplay];
[resultsView setText:resultsToDisplay];
[resultsView setNeedsDisplay];
}
[self dismissModalViewControllerAnimated:NO];
}
I then have a button on the screen that the user uses to pass the data to a simple method.
-(int)AddItemToBasket:(NSString *)ItemNo:(int)QtyToAdd:(NSString *)ItemDescription:(double)SalesPrice:(NSString *)DisplayPrice;
the app runs, the user scans the code, the window gets filled in correctly, there are some fields to hold the split data.
when the use presses the button to call the method.
if i use what i expect to work
Utility *sharedUtility = [Utility sharedUtility];
[sharedUtility AddItemToBasket:(ItemNoToDisplay):(1):(DescriptionToDisplay):(0):(#"1")];
but this does
Utility *sharedUtility = [Utility sharedUtility];
[sharedUtility AddItemToBasket:(ItemNoToDisplay):(1):(Description.text):(0):(#"1")];
The first call always errors with bad access or not a NSCFstring type error.
It seems that the LAST part of the array has some funny char in it that the textfield handles, but the code does not. Some sort of termination issue.
I have worked around it by using the variable from the text box, but am confused as to why i can pass directly the value in the textfield i am passing to the text box ?
Help / Confused..
You are directly assigning to your string ivars instead of going through the synthesized accessors (e.g. should be self.DescriptionToDisplay = ....
This means you are assigning an autoreleased object to the ivar, which may not be around by the time you call your AddItemToBasket method.
As an aside, it is conventional to begin method names and ivar names with lower case letters, and also to name arguments in your methods, that AddItemToBasket method is very difficult to read.
In answer to your comments, it may sometimes work if the area of memory has not been re-allocated since the autorelease. But you definitely need to use the accessors, particularly the setter - this will copy and increase the retain count for you. You could directly use the ivar for the getter in this situation.
You go through all the effort of defining properties but then never use them. This means that you're not retaining the values, and the OS is deallocating them before you actually use them.
So here:
DescriptionToDisplay = [myArray objectAtIndex:2];
You need:
self.DescriptionToDisplay = [myArray objectAtIndex:2];

NSMutableString appendString generates a SIGABRT

This makes no sense to me. Maybe someone here can explain why this happens.
I've got an NSMutableString that I alloc at the top of my iPhone app, then append to later in the process. It results in a SIGABRT, which doesn't add up to me. Here's the code:
Header File (simplified):
#interface MyAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
NSMutableString *locationErrorMessage;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, copy) NSMutableString *locationErrorMessage;
#end
And the relevant parts of the Main:
#implementation MyAppDelegate
#synthesize window;
#synthesize locationErrorMessage;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
self.locationErrorMessage = [[NSMutableString alloc] init];
}
- (void)anotherFunction {
[self.locationErrorMessage appendString: #"Blah Blah Blah"];
}
This all seems simple enough. What am I missing?
I would call this a bug in how property setters are generated, but the answer is pretty simple:
You declared the property as (nonatomic, copy). This means that whenever the locationErrorMessage property is set, it's going to invoke copy on the new value and use that copy as the property value.
Unfortunately, invoking copy on an NSMutableString does not result in an NSMutableString, it results in an NSString (which cannot be mutated using something like appendString:).
So the simple fix would be to change the property declaration from copy to retain.
(I would say that the bug would be: If you declare a property for a mutable object as copy, then the copy setter should actually use mutableCopy and not copy) => rdar://8416047
Your property is copying the passed in string. A copy always is immutable, so you’re trying to send appendString: to an immutable NSString. Declare your property as retain and it will work or write a custom setter that copies the string using mutableCopy.
You also have a memory leak, you should use [NSMutableString string] instead of the alloc-init sequence.
Btw, you have a leak there,
self.locationErrorMessage = [[NSMutableString alloc] init];
you're copying the value, but you never release the actual first allocated NSMutableString.

iPhone SDK - instance variable out of scope issue

I am getting crazy over this error. Compiler is saying out of scope for an instance NSSString variable. Never had this thing before and used thousands of NSString instance variables!
Here is my class .h file
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <CoreAudio/CoreAudioTypes.h>
#import "Snapshot.h"
#interface RecordAudioViewController : UIViewController <AVAudioRecorderDelegate, AVAudioPlayerDelegate> {
NSString *filename;
}
#property (nonatomic, retain) NSString *filename;
- (IBAction) recordAudio;
- (IBAction) playAudio;
#end
Variable is synthesized properly. I initalize filename variable in viewDidLoad method. I want to use it in IBAction method recordAudio, but compiler always says out of scope? Why is that, is this a bug or something?
Here is .m code. viewDidLoad method where I set the filename instance variable:
- (void)viewDidLoad {
[super viewDidLoad];
NSString *tmpDir = NSTemporaryDirectory();
filename = [NSString stringWithFormat: #"%.0f.%#", [NSDate timeIntervalSinceReferenceDate] * 1000.0, #"caf"];
NSLog(filename);
}
And the IBAction method
- (IBAction) recordAudio
{
NSLog(filename); // here I get out of scope message when moving over with mouse cursor and when steping over this line EXC_BAD_ACCESS
}
The entire .m file can be seen here: http://pastie.org/1021993
Actually, if you set filename = [NSString stringWithFormat...], the autoreleased result is NOT retained.
However, if you use self.filename = [NSString stringWithFormat...] it WILL retain the string. Kinda looks like the string is getting released out from under you because you're not retaining it.
You mentioned that you initialize the variable filename in the viewDidLoad method. if you mean nsstring alloc and init methods by initializing, i don't think that you are going the right way. It is not necessary to initialize a synthesized string, or more generically any strings. I'm not sure whether you meant this by initializing, but i gave my opinion based on the idea that i got from your Ques.
Is viewDidLoad actually happening? If it doesn't get called, that would perfectly explain the crash in recordAudio as it hasn't been initialised.

iPhone SDK. How to assign NSString to UILabel text?

If I have a class/object like this
#import <Foundation/Foundation.h>
#interface anObject : NSObject {
NSString *aProp;
}
#property NSString aProp;
#end
and in the cellForRowAtIndexPath method i want to assign one of the string values from its properties to a uilabel of a tableviewcell, how do i do it?
I've tried this but it doesn't work
[[cell topLabel] setText:(NSString *)anObject.aProp];
Putting a breakpoint on the above line and inspecting it, the debugger says "variable is not a CFString
Casting the property to a CFString doesnt work
Modifying the class and declaring the property as a CFStringRef doesnt work either
cell.textLabel.text = #"Foo";
This is the proper way to declare the property:
#property (nonatomic, copy) NSString * aProp;
note: the issue was with your "NSString aProp" not being a pointer.
and, yes, as frog suggested, copy is more appropriate.