Problems Making a Deep Copy of NSMutableAttributedString - iphone

I'm trying to make a deep copy of an NSMutableAttributedString called text using another NSMutableAttributedString called textBackup. Both are properties set to #property (nonatomic, retain), and I create the backup as follows:
NSMutableAttributedString *textBackupTemp = [self.text mutableCopy];
self.textBackup = textBackupTemp;
[textBackupTemp release];
Then later on, when a certain scenario occurs, I need to restore the copy:
NSMutableAttributedString *textTemp = [self.textBackup mutableCopy];
self.text = textTemp;
[textTemp release];
This seems to work fine, however the next time I access text like the following:
[self.text.string characterAtIndex: self.cursor.position-1]
I get the following error:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFString characterAtIndex:]: Range or index out of bounds'
The string size has changed to 1 instead of 43 like it should be. What could possibly be going on here...
EDIT: Updated the problem.

This was just a stupid error on my part, I had put #property(nonatomic, assign) for my textBackup instance variable. I changed it to retain and now everything works.

Related

NSNumber causing a SIGABRT

I have a strange problem (strange if you ask me). Im using a NSNumber object to store a number (doh). When I try to "modify" it it crashes my application. The code I'm using looks like this:
if ([frequency intValue] > 19999)
return;
frequency = [NSNumber numberWithInt:([frequency intValue] + 1)]; //I think this line is causing me the problem
[freqLabel setText:[NSString stringWithFormat:#"%i Hz", [frequency intValue]]];
Where is frequency the NSNumber and freqLabel my label to which I write the value every time this gets called.
Why is this incorrect? It works when I call it for the first time. Is it that NSNumber numberWithInt always returns a new object which I'm trying to assign to frequency?
How do I fix this? Whats the correct way to change a NSNumber's value?
Sorry for my bad english (if there are any mistakes).
EDIT:
The error log looks like this:
[__NSCFType intValue]: unrecognized selector sent to instance 0x73430e0
2012-05-09 16:39:28.064 MyApp[31939:10703] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType intValue]: unrecognized selector sent to instance 0x73430e0'
*** First throw call stack:
(0x17a6022 0x28afcd6 0x17a7cbd 0x170ced0 0x170ccb2 0x4821 0x17a7e99 0x49d14e 0x49d0e6 0x543ade 0x543fa7 0x543266 0x4c23c0 0x4c25e6 0x4a8dc4 0x49c634 0x2e49ef5 0x177a195 0x16deff2 0x16dd8da 0x16dcd84 0x16dcc9b 0x2e487d8 0x2e4888a 0x49a626 0x1cca 0x1c15)
terminate called throwing an exception
But it doesn't always show this error. Sometimes it causes an EXC_BAD_ACCESS. Should I store my variable in a temporary NSNumber?
You can't just alloc/init something once and then you have a lifelong reference to that type. When you assign frequency to numberWithInt, then you are overwriting the previous alloc/init value with an autorelease value (which will be released later and cause the exact behavior you are describing). The reason it works with self.frequency is because your property is set as a retain property, so it automatically retains the autorelease value. Add a retain to your numberWithInt line and it will be fine (or do what you are doing now with self.frequency).
I would try doing this instead
int myNumber = ([frequency intValue] + 1);
frequency = [NSNumber numberWithInt:myNumber];
Yes, numberWithInt: does indeed return a new object. You're probably not retaining this object. Just properly retain frequency when assigning it a new NSNumber. Without context, I'm not sure the best way to accomplish this, but one way is to make frequency a property of your object and using the accessor method.
It would seem that you've already initialized and assigned some value prior to NSNumber, like you have it inside an array for example.
Basically NSNumber objects are immutable, so changing their value is not possible anyway.
You can do it this workaround if you use it inside an array:
NSMutableArray *myOldArray = [[NSMutableArray alloc] init];
myOldInt = 3;
myOldArray[4] = [NSNumber numberWithInt:myOldInt]; // for example
NSMutableArray *myNewArray = [[NSMutableArray alloc] init];
myInt = myOldInt+2;
NSMutableArray *row = [NSMutableArray arrayWithObjects:myOldArray[1],myOldArray[2],myOldArray[3],[NSNumber numberWithInt:myInt],[NSNumber numberWithInt:myInt2],nil];
[myNewArray addObject:row];

Loop through UITextField's and store double values

I'm having trouble looping through a set of dynamically created UITextFields and storing those values as a double to be added to an array later on. I'm still pretty new to obj-c programming so bear with me if this question seems trivial. Thanks. This is what I have so far.
NSMutableArray *textFieldCashArray = [[NSMutableArray alloc] init];
double textFieldCash;
for (int y=0; y<25; y++) {
UITextField *myLabel = (UITextField *)[self.view viewWithTag:y];
textFieldCash = [myLabel.text doubleValue];
[textFieldCashArray addObject:[NSNumber numberWithDouble:textFieldCash]];
}
P.S and here is the error message I'm getting
Pro[962:b303] -[UIControl text]: unrecognized selector sent to instance 0x680f850
2012-04-01 16:05:46.305 iFinance Pro[962:b303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIControl text]: unrecognized selector sent to instance 0x680f850'
*** Call stack at first throw:
I think what's going on here is that the loop variable is being used as the tag, and the loop starts at zero. viewWithTag will answer either the receiver or one of it's subviews with a given tag, so if the view controller's main view has tag==0 (which it probably does), your first text request is being sent to that top-level view.
Try setting the text field tags to some non-zero value, starting at SOME_OFFSET. Then in your loop:
for (y=0; y<25; y++) {
UITextField *myLabel = (UITextField *)[self.view viewWithTag:y+SOME_OFFSET];
// ...
}
danh is most certainly right about the cause and solution to this problem. Just to add a little, cases like this can be somewhat avoided by checking the Class before casting.
if ([[self.view viewWithTag:y] isKindOfClass:[UITextField class]]) {
UITextField *myLabel = (UITextField *)[self.view viewWithTag:y];
textFieldCash = [myLabel.text doubleValue];
[textFieldCashArray addObject:[NSNumber numberWithDouble:textFieldCash]];
}
You should create array at first:
NSMutableArray *textFieldCashArray = [NSMutableArray array];
Edit
Your error log shows that you are receiving some another UIControl object (UIBtton for example) instead of UITextField. Check your tags on xib (or algorith if you set programmatically) and be sure that UITextField objects has corresponding tags

deleting contents of a label - Objective C

I have a label which stores any data that is entered. it has a property and has been synthesized.
#property (strong, nonatomic) IBOutlet UILabel *memoryDisplay;
#synthesize memoryDisplay;
-(void)viewDidLoad
{
[super viewDidLoad];
view.hidden = YES;
}
The label is in a view which is hidden on load
The view has a property and has been synthesized
on the same view there is a button for clearing a label
It has an Action
.h
- (IBAction)clearMemory:(id)sender;
And
.m
- (IBAction)clearMemory:(id)sender
{
self.memoryDisplay.text = #"";
}
However, when ever I try to run the app it crashes and gives me this error
* Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key clearMemory.'
* First throw call stack:
(0x13bc052 0x154dd0a 0x13bbf11 0x9b3032 0x924f7b 0x924eeb 0x93fd60 0x23291a 0x13bde1a 0x1327821 0x23146e 0xd8e2c 0xd93a9 0xd95cb 0x39a73 0x39ce2 0x39ea8 0x40d9a 0x11be6 0x128a6 0x21743 0x221f8 0x15aa9 0x12a6fa9 0x13901c5 0x12f5022 0x12f390a 0x12f2db4 0x12f2ccb 0x122a7 0x13a9b 0x1b28 0x1a85)
terminate called throwing an exceptionsharedlibrary apply-load-rules all
I also tried to add some if statements to check for possible problems:
- (IBAction)clearMemory:(id)sender
{
if (!view.hidden) {
if ([memoryDisplay.text length] > 1)
{
self.memoryDisplay.text = #"";
}
}
Can anyone tell me what the problem might be?
If I take off everything got to do with the clear button it all works perfectly.
Thanks :)
Check all IB outlet bindings for broken links
Check to see if your memoryDisplay property is connected from the viewController to the UILabel in the nib/xib.
I couldn't do it the way I wanted, however I did find a workaround.
I placed a hidden, uneditable text box in the view which was empty and set the labels text to the text box whenever the clear button was pressed.

string to another view

I've followed tutorial from
passing data between classes
did it, and is working fine, but I really need to send some string value (a date) to another view, (is simple I know but Im a noob for this!),
so my problem is that I need to send this string (Date), to the other view, the string is ok, but I dont seem to get (yet)how to construct the function,
I get no warnings but the app breaks,
- (void)calendarView:(KLCalendarView *)calendarView tappedTile:(KLTile *)aTile{
NSLog(#"Date Selected is %#",[aTile date]);
string1 = [[aTile date] description];
dateis.text = string1; //label to check string is working
NSLog(#"ahi va! %#", string1);
NSString *cucux = dateis.text;
CroTime *croco = [CroTime alloc];
croco.string1 = cucux;
[self.view addSubview:croco.view];
NSLog(#"croco = %#", cucux);
}
Console message
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CroTime setString1:]: unrecognized selector sent to instance 0x5e2e1e0'
Thanks a lot!!
setString1 is a setter method which calls when you set a property value.So from your error this looks like you make a property string1 in class CroTime but you did not synthesize it.
This type of condition occur only in this case. so ensure that when you make property then synthesize it in .m file.

Filling an NSMutableArray with a Set of Classes and Then Getting them Back

Hopefully I can make this clear, but I am new to Objective-C and to be honest not great with Arrays in the first place.
So, I have a Singleton class (called SingletonState) that I am using to pass variables around my app (please can we leave the whether I should use Singleton classes out of this - I will fix that later). In this class, I have an NSMutableArray (called arrayMyEvents). I have also created a class that I am going to store a list of events (called EventClass). When the user logs in, I call a web service and get back 3 strings. The 3rd string is a comma separated list of value. I parse the data and populate the custom class EventClass. I then add this class to the SingletonState.arrayMyEvents.
I have all of this working. I can go to another ViewController and access the "count" of items in arrayMyEvents.
PROBLEM: I now want to edit one of the ScheduledEventsClass"es" in my array. How do I get access to it and edit some of the properties and update the one in my SingletonState class?
Here is some of the code, that I've tried:
NSString *sWebServiceEvents = [[NSString alloc] initWithFormat:#"%#", [result objectAtIndex:2]];
if ( [ sWebServiceEvents isEqualToString:#"NULL" ] != true ) {
NSArray *arrayEvents = [sWebServiceEvents componentsSeparatedByString:#","];
// If the array has not been initialized they initialize it.
if (sharedState.arrayMyEvents == nil) {
sharedState.arrayMyEvents = [[NSMutableArray alloc ] init ];
}
for (NSString * sEvent in arrayEvents) {
// Set equal to the value of the array (the Event Number) at the same
// position as the row that we are being asked to return a cell/row for.
EventClass *eventClass = [[EventClass alloc] retain];
eventClass.sEvent = sEvent;
[ sharedState.arrayEvents addObject:eventClass ];
}
NSLog(#"LoginView - sharedState.arrayMyEvents Count: %d", [sharedState.arrayMyEvents count]);
}
Here is me trying to access it in another ViewController:
EventClass *eventClass =
[sharedState.arrayMyEvents objectAtIndex:row ];
NSLog(#"eventClass.sEventNumber: ", eventClass.sEventNumber);
eventClass.sLocation = #"Jason's Big Location";
You're going to have some memory leaks from the sEvent loop. [[EventClass alloc]retain] leaves you an uninitialized EventClass object with a reference count of 2. You'll need to change that to [[[EventClass alloc] init] autorelease] to keep it from leaking. The arrayEvents NSMutableArray will retain it during the addObject: call. (Shouldn't that be [sharedState.arrayMyEvents addObject: eventClass] in the loop?)
After that, all you have to do to edit the EventClass object in the second block of code is edit it. The eventClass variable is a pointer to an object in the array. Anything done to that object doesn't affect the pointer referencing it, it affects data referenced by it. The code you have in the second block should change the sLocation of the selected object as you intend.
You have a few more memory leaks in there, too. Use Cmd-Shift-A to build with the static analyzer and it'll tell you where.
Maybe the problem is that you put them in sharedState.arrayEvents but try to take them out of sharedState.arrayMyEvents. Different variables.
Also, lots of memory leaks.
Thanks John and St3fan, your answers and time are appreciated!!!
I think that I figured out my issue:
Basically, the class that I created (EventClass) had the properties setup like this:
#property (nonatomic, assign) NSString *sStudyNumber;
#property (nonatomic, assign) NSString *sTheater;
but, they should be (or at least I got it to work like this):
#property (nonatomic, retain) NSString *sStudyNumber;
#property (nonatomic, retain) NSString *sTheater;
Then, in my second view I was able to do this:
EventClass *eventClass = [sharedState.arrayMyEvents objectAtIndex:row ];
NSLog(#"MyEvents: %#", eventClass.sEventNumber);
eventClass.sLocation = #"Jason's Big Location";
I then checked it in another method of the view using this and it was still there:
EventClass *eventClass = [sharedState.arrayMyEvents objectAtIndex:row ];
NSLog(#"MyEvents: %#", eventClass.sEventNumber);
NSLog(#"MyEvents: %#", eventClass.sLocation);
I also, checked it in yet another view and the value was maintained in the SharedState.arrayMyEvents without issue. :)
In the end, I believe that I boiled down to the difference between "assign" and "retain".
Now, on to the memory leaks :(
Please, let me know if you see any other issues with this.
Thanks,
Jason