CFString isNaturallyRTL - message sent to deallocated instance - iphone

I already googled for "CFString isNaturallyRTL" with 0 results.
these are my classes:
//in .H
#interface myViewController : UIViewController {
UITextField *from;
UITextField *to;
NSString *fromText;
NSString *toText;
}
#property (nonatomic, retain) NSString* fromText;
#property (nonatomic, retain) NSString* toText;
#property (nonatomic, retain) UITextField *from;
#property (nonatomic, retain) UITextField *to;
//in .m
#synthesize from, to;
#synthesize fromText, toText;
viewDidLoad(...) {
fromText = #"Roma";
toText = #"Lecce";
}
- (void) drawRoute {
if ( ([[from text] length] > 2) && ([[to text] length] > 2) )
{
fromText = from.text;
toText = to.text;
[...]
}
}
Now, i have a view that open on button touch tha contains two text boxes and a button. Like this.
- (void) drawRouteTextboxes {
from = [[UITextField alloc] initWithFrame: [...] ];
from.text = fromText;
from.delegate = self;
[ctr.view addSubview:from];
[from release];
to = [[UITextField alloc] initWithFrame: [...] ];
[...]
[searchButton addTarget:self action:#selector(drawRoute) forControlEvents: UIControlEventTouchUpInside];
}
It's all correct, compile and run.
First time that i click drawRouteTextboxes, it opens my view with default text setted ("Roma" and "lecce").
Second time, i open the view, edit textfield and call drawRoute. It's ok.
The third time that i call drawRouteTextboxes it return me this runtime error:
*** -[CFString _isNaturallyRTL]: message sent to deallocated instance 0x3a8d140
I don't know where is the problem...
Someone know a solution?
It's the first time that i see this error!
thanks,
Alberto.

It's all correct, compile and run.
If it was all correct, it would run without error. ;)
This looks suspect:
fromText = from.text;
toText = to.text;
If from.text and to.text are returning either autoreleased objects or objects that are later released, then the above doesn't retain the strings and could easily lead to an over-release problem as you are seeing.
Use self.fromText = from.text; instead.
Note that NSString* properties should almost always be copy and not retain.

Related

Can't update UIlabel text

In my iPhone App I have ViewController1 that contains a UILabel called selectedBook and I added to its view in the storyboard and I connected the outlets.
I have another view controller called ViewController2 and I pass some data from ViewController2 to ViewController1 and the text of the UILabel is one of them.
In ViewController1 I wrote the following code:
in ViewController1.h
#interface ViewController1 : UIViewController{
IBOutlet UILabel *selectedBook;
NSString *BookName;
NSString *BookCover;
NSString *BookAbout;
}
#property (nonatomic, retain) NSString *BookName;
#property (nonatomic, retain) NSString *BookCover;
#property (nonatomic, retain) NSString *BookAbout;
-(void) setTheSelectedBook:(NSString *)bookName bookCover:(NSString *)bookCover bookAbout:(NSString *)bookAbout userID:(int)userID;
In ViewController1.m
-(void) setTheSelectedBook:(NSString *)bookName bookCover:(NSString *)bookCover bookAbout:(NSString *)bookAbout userID:(int)userID
{
BookName = bookName;
BookCover = bookCover;
BookAbout = bookAbout;
NSLog(#"in setTheSelectedBook, BookName: %# - BookCover: %# - BookAbout: %# - userID: %d", BookName, BookCover, BookAbout, userID);
selectedBook.text = bookName;
}
In viewController2 I wrote the following code:
ViewController1 *viewController = [self.storyboard instantiateViewControllerWithIdentifier:#"Suggest"];
[viewController setTheSelectedBook:#"MybookName" bookCover:#"MyBookCover" bookAbout:#"MuBookAbout" userID:1];
My data pass properly and I see it in the output, but the problem is that the UILabel text is not updated, meaning that this line
selectedBook.text = bookName;
does not work.
I tried writing it as selectedBook.text = #"someText"; and this works.
I can't understand what's missing to make selectedBook.text = bookName; work? I hope someone help me in this issue.
Thanks in advance.
Your code is a bit confusing. Never begin your variables with a capital letter. BookName should be bookName or _bookName. I think that's one of the problems.
If you instead create a Book-class with the variables:
#property (copy) NSString *name;
#property (copy) NSString *cover;
#property (copy) NSString *about;
Then in your viewController1 create the book by instantiating your Book-class and set it's variables, you could then send your Book-object to your viewController2.
That would probably be easier for you.
If you just want to change your own code, you could try with:
In your h-file:
#property (copy) NSString *BookName;
#property (copy) NSString *BookCover;
#property (copy) NSString *BookAbout;
And in your m-file:
-(void) setTheSelectedBook:(NSString *)bookName bookCover:(NSString *)bookCover bookAbout:(NSString *)bookAbout userID:(int)userID
{
_BookName = bookName;
_BookCover = bookCover;
_BookAbout = bookAbout;
NSLog(#"in setTheSelectedBook, BookName: %# - BookCover: %# - BookAbout: %# - userID: %d", BookName, BookCover, BookAbout, userID);
selectedBook.text = _BookName;
}
The only explanation I see here is, that bookName is not an NSString.
Add the following line to that method:
NSLog(#"bookName class is %#", [bookName class]);
NSLog(#"selectedBook class is %#", [selectedBook class]);
What does XCode write to the log during runtime?

objective c and xcode assigning value to label coming from a NSInteger type variable

It's me again, I've been struggling with this for the past hour and a half and can't seem to find a good way of implementing this. I'm basically trying to display results on a label on clicking of a button. (Just starting out with xcode so I'm not sure if that's the right term for that action). Anyway, here's my code and the method on my controller: I have
#interface Match : NSObject{
}
#property NSInteger *Id;
#property NSString *fighter1, *fighter2;
- (id) initWithWCFId:(NSInteger)matchId bracketId:(NSInteger)bracketId;
#end
#implementation Match
- (id) initWithWCFId:(NSInteger)matchId bracketId:(NSInteger)bracketId{
self = [self init];
if(self){
self.Id = &(matchId);
self.fighter1 = #"Person 1";
self.fighter2 = #"Person 2";
}
return self;
}
#end
--- controller ---
#interface ViewController : UIViewController{
/*IBOutlet UITextField *txtFieldBracketId;
IBOutlet UITextField *txtFieldMatchId;*/
}
#property (weak, nonatomic) IBOutlet UITextField *txtFieldBracketId;
#property (weak, nonatomic) IBOutlet UITextField *txtFieldMatchId;
- (IBAction)btnSubmit:(id)sender;
#end
--- implementation
- (IBAction)btnSubmit:(id)sender {
#autoreleasepool {
Match *match = [[Match alloc]initWithWCFId:[_txtFieldMatchId.text integerValue] bracketId:[_txtFieldBracketId.text integerValue]];
self.lblMatchId.text = [[NSString alloc] initWithString:[NSNumber numberWithInt:match.Id]];
self.lblFighter1.text = [[NSString alloc] initWithString:match.fighter1];
self.lblFighter2.text = [[NSString alloc] initWithString:match.fighter2];
}
}
I basically have two text boxes.
Now when I click the button it'll get the value for those text boxes and then displays the data it got based off of those inputs. It'll then display the three following data:
Id, Fighter1 and Fighter2.
So what's happening is, when I click the button, the whole thing stops and gives me this error:
NSInvalidArgumentException', reason: '-[__NSCFNumber length]:
unrecognized selector sent to instance 0x74656e0'
* First throw call stack: (0x1c90012 0x10cde7e 0x1d1b4bd 0x1c7fbbc 0x1c7f94e 0xae4841 0x2891 0x10e1705 0x18920 0x188b8 0xd9671 0xd9bcf
0xd8d38 0x4833f 0x48552 0x263aa 0x17cf8 0x1bebdf9 0x1bebad0 0x1c05bf5
0x1c05962 0x1c36bb6 0x1c35f44 0x1c35e1b 0x1bea7e3 0x1bea668 0x1565c
0x23dd 0x2305) libc++abi.dylib: terminate called throwing an exception
Now I'm not sure if 1. The way I designed my class is correct, using "NSInteger" for the property id. or
2. Assigning the Id integer to string (edit box) is wrong.
Two things:
The property should not be pointer type, so it should be #property NSInteger Id; and in init it should be just self.Id = matchId;
Make it to string by using [NSString stringWithFormat:#"%d", match.Id]
In addition to the issues with your Id property, the crash is coming from this:
self.lblMatchId.text = [[NSString alloc] initWithString:[NSNumber numberWithInt:match.Id]];
You are trying to pass an NSNumber object as the argument to the initWithString: method. But this method expects an NSString value, not an NSNumber.
Update the three lines to:
self.lblMatchId.text = [[NSString alloc] initWithFormat:#"%d", match.Id];
self.lblFighter1.text = match.fighter1;
self.lblFighter2.text = match.fighter2;
I'm assuming match.fighter1 and match.fighter2 are NSString properties.

Change text in UILabel with NSMutablearray data

I'm trying to change the text of a UILabel with text from an array upon a button click, but it doesn't do anything.
#interface Test01AppDelegate : NSObject <UIApplicationDelegate> {
UILabel *helloLabel;
UIButton *hellobutton;
NSMutableArray *madWords;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UIButton *hellowButton;
#property (nonatomic, retain) IBOutlet UILabel *hellowLabel;
#property (nonatomic, retain) NSMutableArray *madWords;
- (void) madArrays;
- (IBAction)helloYall;
#end
and
#import "Test01AppDelegate.h"
#implementation Test01AppDelegate
#synthesize window = _window;
#synthesize hellowButton;
#synthesize hellowLabel;
#synthesize madWords;
- (void) madArrays {
[madWords addObject:#"Part A"];
[madWords addObject:#"Part B"];
[madWords addObject:#"Part C"];
[madWords addObject:#"Part D"];
}
- (IBAction)helloYall {
[self madArrays];
self.hellowLabel.text = [madWords objectAtIndex:0];
}
I can set the helloLabel text with
#"some text here";
and it works fine. Also, I tried copying the "madArrays" method into the "helloYall" method and it still didn't work. As I said, I can manually set the text and it works, but I'd like to pull the info from an array. Eventually, I'd like to loop through the array to grab the text on each button press, but one step at a time. Thanks.
You never create the madWords array. You need to add:
self.madWords = [NSMutableArray array];
at the top of:
- (void) madArrays {
would probably be a good place. Other possibly good places would be i the class init method or the view controller viewWillAppear method.
// Or you can try this in your init Method:
//first allocate the ivar
- (void)myInitMethod {
madArrays = [[NSMutableArray]alloc]init];
}
//then you can do anything directly to the ivar or throughout de setter
- (void)doAnythingWithiVar {
// do your stuff
}
//when you are done you can dealloc your ivar
- (void)dealloc {
[madArrays release];
[super dealloc];
}
It looks like madArrays is still nil when you come to populate it. At some point you need something like [self setMadArrays:[[NSMutableArray alloc] init]];. Also, don't forget to release madArrays in the dealloc before calling super as you'll have a memory leak.

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];

Storing and opening data in a variable

I'm making an app that calculates certain things.
I need it to be able to take the input from the first textfields, for example 4+4 and save the result in a variable.
In the second text fields there could be 8+8 for example, and the result of that will also be saved into a variable (possibly the same).
Third row of textfields could yield more numbers etc, etc..
In the end there will be a button "Calculate" for example. And that will take the results from first, second, third etc textfields and calculate all of those together and output the end result.
The calculations are of course more advanced than this, but I just need the basic/simple idea of how to do this.
There is no need for saving the data to a file just now, it should just be in the app while the other textfields are being filled.
For 0x8badf00d:
Header.
#interface UnitConverterViewController : UIViewController {
NSMutableArray *calculationsArray;
UITextField *m1Text;
UITextField *m2Text;
}
#property (nonatomic, retain) IBOutlet UITextField *m1Text;
#property (nonatomic, retain) IBOutlet UITextField *m2Text;
#property (nonatomic, retain) IBOutlet NSMutableArray *calculationsArray;
#end
Implementation:
#implementation UnitConverterViewController
#synthesize m1Text, m2Text, calculationsArray;
#synthesize resultTotal = _resultTotal;
-(id)init {
if(self = [super init]){
calculationsArray = [[NSMutableArray alloc] init];
}
}
- (void)compute{
NSString* sumString = [NSString stringWithFormat:#"%d",[m1Text.text intValue]+[m2Text.text intValue]];
[calculationsArray addObject:sumString];
}
-(IBAction)calculate{
int total=0;
for(NSString* sumStr in calculationsArray){
total = total+[sumStr intValue];
}
NSLog(#"Total: %d", total);
[calculationsArray release], calculationsArray = nil;
}
I must be doing something wrong, and I know I need a way to output this, a label and such. But for now I need to know if what I've done so far is correct, and what the best way of outputting it would be.
You should declare the variables to store the results in your header file, these are than accessible from anywhere in your .m file, the same goes for your text fields.
For example:
Calculator.h
#interface Calculator: SuperclassName{
UITextField *_fldOne;
UITextField *_fldTwo;
UITextField *_fldThree;
UITextField *_fldFour;
int resultOne;
int resultTwo;
int _resultTotal;
}
#property(nonatomic, readonly) int resultTotal;
- (void) calculate;
#end
Calculator.m
#implementation Calculator
#synthesize resultTotal = _resultTotal;
- (void) calculate{
resultOne = [_fldOne.text intValue] * [_fldTwo.text intValue];
resultTwo = [_fldThree.text intValue] / [_fldFour.text intValue];
totalResult = resultOne + resultTwo;
}
#end
In this example resultOne and Two, and all the textfields are available throughout your class to work with, the totalResult is set as a readonly property and synthesized to create a getter automaticaly (which returns the value stored in _totalResult because of synchronizing like totalResult = _totalResult) as so it is available to read from outside the class.
As long as it all happens on one screen it should be more than enough, but of course you could make an NSDictionary or NSArray but that seems unnecessary here.
Hope this helps
Save the result to array. Lets say you have NSMutableArray* calculationsArray;//iVar
//initialize calculationsArray in init method
-(id)init
{
if(self = [super init])
{
calculationsArray = [[NSMutableArray alloc] init];
}
}
- (void)compute
{
NSString* sumString = [NSString stringWithFormat:#"%d",[textField1.text intValue]+[textField2.text intValue]);
[calculationsArray addObject:sumString];
}
- (IBAction)calculate
{
int total=0;
for(NSString* sumStr in calculationsArray)
{
total = total+[sumStr intValue];
}
NSLog(#"Total: %d",total);
[calculationsArray release],calculationsArray = nil;
}