'NSStringFromClass(Class _Nonnull __unsafe_unretained aClass)' method in iOS10 device will crash! --[CFString release] - ios10

My code is [tableView dequeueReusableCellWithIdentifier: [NSStringFromClass([xxx class])] to return cell, but it will crash in iOS10 device.

usually you must specify a literal, AND set manually the same in Storyboard.
Are you using all in code?
are you using the name of class instead of it?

is wrong to use class name as ID, as in Storyboard You must set it.
if You write:
NSString * ID = NSStringFromClass([CustomCell class]);
Yu got a string, but is NOT the ID.

Related

passing values to another method

I am setting the value for the string in the viewdidload method and getting the string value in the button action method the app gets crashed. can I know the reason for crashing and how to pass the values to the method.
in .h file
NSString *test;
in .m file
-(void)viewDidLoad
{
test = [NSString stringWithFormat:#"sample"];
}
-(IBAction) buttonPressed:(id)sender
{
NSLog(#"%#", test);
}
When I pressed the button the app crashes.
Please try using this solution, I think this will help you,
Create Property of test in .h file like this,,
#property(nonatomic,retain) NSString *test;
and synthesize it in .m file like this,
#synthesize test;
now use test as self.test in .m file like this,
-(void)viewDidLoad
{
self.test = [NSString stringWithFormat:#"sample"];
}
-(IBAction) buttonPressed:(id)sender
{
NSLog(#"%#", self.test);
}
Another solution for this is just retain that test string in ViewDidLoad also, I think this will also help you..
Hope this will help you..
Let me try to explain it in more detail:
You have a string variable in .h file. In view did load you are assigning it as:
test = [NSString stringWithFormat:#"sample"];
What actually happning in this code is your test is a autoreleased object. When you use this and object without alloc and init this is autoreleased object and will release memory after the method you occupied it.
For avoiding this situation you can use #Mehul's solution by creating property. This is against encapsulation concept. Sometimes you have objects you don't want to access outside of the class or don't want to show with objects. Use following in those conditions:
test = [[NSString stringWithFormat:#"sample"] retain]; // or
test = [[NSString alloc] initWithFormat:#"sample"];
this will keep your string alive till you release it.
There is another way that is not good to use but want to tell you so you can understand it better. Using
test = #"sample";
If you don't want to append string or use it with format you can assign simple string to you NSString object.
using this will have a infinite retainCount of your test variable. You can use this to avoid crash but this is not preferable because as I told this have a infinite retaiCount you can't release it and free your memory after use. So earlier methods are more correct.
This is true with all of your autorelease objects which are created with class methods and not with init.
Hope this will clear you more.
I think simple allocation will solve the problem. Just replace this code in the viewDidLoad method
-(void)viewDidLoad {
test=[[NSString alloc]initWithString:#"Sample"];
}

App crash in button

I have face strange problem on UIButton.
When i tap button the app is crash .
I wrote below code for that...
-(IBAction)renameTest:(id)sender
{
NSLog(#"Tapped");
// UIButton *button = (UIButton *)sender;
NSUInteger row = 1;//button.tag;
NSString * titlename = [titleArray objectAtIndex:row];
RenameTest *renameVC = [[RenameTest alloc]initWithNibName:#"RenameTest" bundle:nil];
renameVC.titlespell = titlename;
NSLog(#"titlespell = %#",renameVC.titlespell);
NSLog(#"title = %#",titlename);
// [button release];
[self.navigationController pushViewController:renameVC animated:YES]; //here APP is cresh
[renameVC release];
}
I check also my .Xib file name .It is ok and files are there.
error msg is below :
2012-07-11 14:28:29.079 TestApp[238:207] -[__NSCFDictionary _isNaturallyRTL]: unrecognized selector sent to instance 0x73d8a80
Thanks in Advance.
[button release] is causing the problem. Remove it and check.
_isNaturallyRTL is an NSString method (private), and it looks like you are passing a dictionary instead of a string somewhere.
Breaking on the exception and showing us the call stack at that point would help tremendously.
If u have created the button in xib file then u cannot release it because you have not allocated it and claimed ownership.. U should call release only on objects you have allocated by calling alloc..
Remove the [button release] statement .. that should fix the crash!
You have a crash which is related to a dictionary and your titlename string is set equal to, titleArray objectAtIndex:row. I believe, without seeing the declaration of your variables, that titleArray is a dictionary, or is a NSMutableArray importing from a plist of dictionaries, either way you need to use objectForKey, when using dictionaries, like this:
[[titleArray objectAtIndex:(NSUInteger *)] objectForKey:(NSString *)]
Obviously replace (NSUInteger *) with your integer row and (NSString *) with the name of your key. This may not be the answer but from your crash report and visible code, this is what I assume.

sender currentTitle not recognized in Xcode

I am doing something as simple as get the current title of an UIButton
-(IBAction)buttonPressed:(id)sender{
NSString *someString = [sender currentTitle];
}
For some reason Xcode confuses sender with self because the complete options only retunrns methods that belongs to "self". No matter whatever I do, I get the same
-(IBAction)buttonPressed:(id)sender{
UIButton *btn = (UIButton *)sender;
NSString *someString = [btn currentTitle];
}
Xcode crashes in NSString line with "unrecognized selector sent to btn". I can copy working code from another project and the same happens. What could be wrong?
SOLUTION
Resarted XCode and it worked fine o.O
I don't think Xcode is confusing sender with self, what's happening is that Xcode knows nothing about sender, since it was declared as id, which means Xcode only knows it's an object.
The suggestions you are getting in Xcode are probably just methods every object has.
You have to make sure that sender is what you expect, try doing this and checking if it's actually a button or something else:
-(IBAction)buttonPressed:(id)sender{
NSLog(#"Sender: %#", sender);
}
SOLUTION
Resarted XCode and it worked fine o.O
Possible reason for "unrecognized selector sent to btn" is when you call a method which was not actually written. Or you might be called
[self buttonPressed];
instead of
[self buttonPressed:];
Your problem is your calling the 'id' not the button its self. Replace the 'id' with 'UIButton' like so...
-(IBAction)buttonPressed:(UIButton *)sender{
NSString *someString = sender.currentTitle;
}
I think the problem is wrong syntax
It is
sender.currentTitle;
OR
btn.currentTitle;
currentTitle is a property and there is no function called currentTitle and hence you get the error "unrecognized selector sent to btn".

Problem with class methods in objective c

i have a tableview controller like so,
NSString *selectedindex;
#interface ContactsController : UITableViewController<ABPeoplePickerNavigationControllerDelegate> {
NSMutableArray *names;
NSMutableArray *phonenumbers;
NSMutableArray *contacts;
DatabaseCRUD *sampledatabase;
}
+(NSString *) returnselectedindex;
#end
in the implementation file i have
+(NSString *) returnselectedindex
{
return selectedindex;
}
when a row is selected in the tableview i put have the following code.
selectedindex = [NSString stringWithFormat:#"%d", indexPath.row];
NSLog(#"selected row is %#",selectedindex);
in a different class i am trying to access the selectedindex. like so
selected = [ContactsController returnselectedindex];
NSLog(#"selected is %#",selected);
it gives me a warning: 'ContactsController' may not respond to '+returnselectedindex'
and crashes. i am not sure why. i have used class methods previously lot of times , and never had a problem. any help please. Thank You.
The reason you're crashing is that you're assigning a value to a global variable (selectedindex), but you're never taking ownership of it by calling -retain. As a result, the string doesn't know you need it to stay around, so the system deallocates it. Later, when you try to access it, it's already deallocated.
In order to avoid the crash, you need to add a retain call when you assign the value. Of course, since a selected index is something that's likely to be changing often, you'd want to release the previous value before overwriting it and retaining the new one. Thus, you should have this code:
[selectedindex release];
selectedindex = [[NSString stringWithFormat:#"%d", indexPath.row] retain];
That will fix your crash.
Now that your crash is fixed, though, you should really rethink your design. There's no reason for selectedindex to be a global variable; since the selected index is very likely specific to that instance of your ContactsController, it should be an instance variable of that class. Instead, you've declared it as a global variable, which means that there is only one selectedIndex shared between ALL instances of ContactsController. Then, in turn, your +returnselectedindex method should be an instance method, not a class method. (It should also be renamed in order to follow Cocoa naming conventions, but that's well off topic.)
I think You didn't allocated the selectedindex NSString.Thats why it will not crash on the same class and it crashes in the new Class.
So You allocate it in the setter method of "returnselectedindex"
Otherwise, Copy or retain the receive value of returnselectedindex value like this,
selected = [[ContactsController returnselectedindex]copy];

Custom Classes not being retained

I have a NSXMLParser that parses YT's API. It then turns all the videos into a class I wrote called video. Then the class is put into an array. Then back at my rootviewcontroller I access xmlParser.allVideos (xmlParser is a class I wrote that is the delegate for the xml parser.
Here is what I do with it in the viewDidLoad:
arrayFromXML = xmlParser.allVideos;
Then in the drawing of the tableView I do this:
tempVideo = [arrayFromXML objectAtIndex:indexPath.row];
cell.textLabel.text = tempVideo.videoTitle; //crashes here and says NSString has been deallocated.
How can I fix this?
If arrayFromXML is an instance variable you have to retain or copy (to be safe from later manipulation) the array as xmlParser simply might not be alive anymore when other methods are called later:
arrayFromXML = [xmlParser.allVideos copy];
Or better yet using a copy or retain property:
self.arrayFromXML = xmlParser.allVideos;