PFObject as MKAnnotation causes NSInternalInconsistencyException - ios

I have a subclass of PFObject that conforms to MKAnnotation, which is working fine for adding annotations to a map view, and starting to drag annotations, but as soon as I stop dragging, the app crashes with the following exception:
NSInternalInconsistencyException', reason: 'Getter should take no arguments!
I've tracked the crash down to [PFObject forwardInvocation], which leads to [PFObjectSubclassingController _forwardGetterInvocation:forProperty:withObject:], but I can't figure out what's calling the forwardInvocation.
Anyone have any ideas?

Problem found... I needed to declare a setCoordinate:(CLLocationCoordinate2D)newCoordinate method in the custom class. Whoops.

Related

Crash when tapping on UITextField or UITextView: Assertion failure in -[UICalloutBarButton layoutSublayersOfLayer:]

Answers to similar questions did not help so I'm opening a new question.
I have this issue in several of my projects now:
Tapping on the first responder UITextField or on a UITextView (that would normally bring up the default context menu for copy and paste) causes my app(s) to crash.
Note: UICalloutBarButton is a UIKit private API, so there is no chance to change its layoutSubviews implementation and call [super layoutSubviews] in it.
Log message:
*** Assertion failure in -[UICalloutBarButton layoutSublayersOfLayer:], /SourceCache/UIKit_Sim/UIKit-2935.137/UIView.m:8794
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. UICalloutBarButton's implementation of -layoutSubviews needs to call super.'
I don't have any idea why this appears. Would be very happy for any help.
EDIT
Trying to reproduce the crash in a demo project I found out that it is a custom UIView category that causes the crash. See accepted answer for more details.
By trying to reproduce the crash in a demo project I found out that it is a custom UIView category that causes the crash.
When creating this fault, I assumed that category methods were only available for UIView subclasses that #import this category.
But this is not the case as described in the Apple Documentation:
Any methods that you declare in a category will be available to all instances of the original class, as well as any subclasses of the original class. At runtime, there’s no difference between a method added by a category and one that is implemented by the original class.
So not being aware of this, I had overridden in my custom category the UIView method + (BOOL) requiresConstraintBasedLayout to return YES (like I wrongly thought only for those classes that import this category).
Putting a breakpoint in that method, I learned that this 'category-method' gets called from everywhere in the UIKit once the category files are added to a project.
Nothing but a painful way of learning/accepting, that overriding methods in categories is never a good idea - unless you want to cause strange crashes like this ;-).
Sorry for stealing your time, but thank you for asking some code examples which forced me to seriously reproduce the problem - which lead me to the crash-cause...

[Not A Type release]: message sent to deallocated instance in UITableView

I get a crash when quickly scrolling through a UITableView.
The crash is :
*** -[Not A Type release]: message sent to deallocated instance 0x1aded1c0
And the exception breakpoint indicates this as the source:
- (void)updateInfo:(Item*)item{
if (!item) {
return;
}
Program *prg = (Program *)item;
self.titleLabel.text = prg.title; // crash here
self.descriptionLabel.text = prg.item_description;
}
This method is inside the custom table view cell subclass and it is called when data from the internet is ready to be displayed in the cell.
I have never seen this kind of crash before...
What is the best way to fix it?
Reason:
As you scroll the tableview, the cells on top get released and when data comes from internet for those cell, they have already been released. So, change your logic. You generally receive this error when you try to access and update an object which has already been released. As you are saying that this method is inside your custom cell, so it clearly indicates that the cell is already released. That's why your app gets crashed when you try to update any view of that cell.
Solution:
Either you can reload your whole tableview or reload those specific cells when you receive data.
Where are you calling the method "updateInfo"? You should call this in cellForRowAtIndexPath. You should not store a reference to a cell and then call this.
When you get the data from your service you just call self.tableView reloadData (or perhaps reloadRowsAtIndexPaths if you know which row needs update and dont want a performance hit)

BAD_ACCESS & NSInvalidArgumentException in working with UIPickerView

I'm beginner in working with UIKit. When I tired to work with UIPickerView in iOS 7 SDK I faced to a hard-to-solve problem.
We know that UIPickerView needs two resources to work perfectly: dataSource & delegate. So I wrote a class named "KMPickerProtocols". I adopted it to UIPickerViewDataSource & UIPickerViewDelegate Protocols and then I added some extra setter methods to it.
KMPickerProtocols is adopted to all of essential methods of dataSource and delegate Protocols + the necessary optional method for setting title of each row (pickerView:titleForRow:forComponent). all these methods get things done without any problem (in normal situations).
Finally I set the delegate and dataSource properties of my UIPickerView (named _accountPicker) manually with this piece of code:
NSArray *delegateAgent = [[KMTwitterDelegate new] run ];
_accountPicker.dataSource = [delegateAgent objectAtIndex:0];
_accountPicker.delegate = [delegateAgent objectAtIndex:1] ;
(the run method is designed to set some properties including height of each row and ...)
Now, when I run my app it will show my twitterAccounts stored in system(accounts framework) in the form of UIPickerView. but there is a problem: As soon as I scroll the picker view or tap on any row, the program will crash and I get BAD_ACCESS (code=2 , address = 0x1) in this line of code:
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([KMAppDelegate class]));
}
and also there are sometimes that I get signal SIGABRT in above line. in these situations Log Says:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSMallocBlock pickerView:titleForRow:forComponent:]: unrecognized selector sent to instance 0x8c75ad0'
I will appreciate if you help me to deal with this problem. I really don't know where the bug is. but I guess this screen shots will be helpful to find that:
The problem in above screenshot is that UIPicker called the "pickerView:titleForRow:forComponent" method for row 0, three times instead of one time. I just don't know whether that is this because myFault or because the typical behaviour of UIPicker.
You are sending the message pickerView:titleForRow:forComponent: to an object of type NSMallocBlock. This is a message automatically sent from the picker to its datasource, so my assumption is that you are assigning the datasource:
_accountPicker.dataSource = [delegateAgent objectAtIndex:0];
The datasource you assign does not conform the protocol UIPickerViewDataSource
Checkout what returns [delegateAgent objectAtIndex:0]; and make sure it is what you expect it to be.

SIGABRT between loadView and viewDidLoad

I'm new to iOS development, and having trouble identifying the source of a SIGABRT. I've narrowed it down to something that happens during initialization of a UIViewController subclass, specifically between its loadView and viewDidLoad methods.
I narrowed it down with an NSLog call in each of those method overloads in my UIViewController subclass. However, I'm unable to get any more granularity from the debugger; I can't step into the [super loadView] method. Are there any techniques for debugging a SIGABRT other than NSLogs and stepping through with the debugger? Is there any way to see exactly from where the exception was thrown?
Program-specific details
This program uses Core Data; I created it following this tutorial. I believe the SIGABRT started happening after I ran through the "Relationships In Action" section, and was not happening before (I think I had a successful build at the end of the preceding section). Specifically, I can successfully add a Person, but PersonDetailTableViewController throws a SIGABRT between loadView and viewDidLoad.
I've tried deleting the app from the iPhone simulator, and also running a Product > Clean; neither had any effect.
Found the culprit. I had earlier incorrectly created an IBOutlet by ctrl+dragging from a Table View Cell's Text Field into my UITableViewController subclass. I manually deleted the code this created, but a connection remained in the Connections Inspector. Deleting that connection solved the problem -- no more SIGABRT.
It's unfortunate that Xcode sees the problem (see the exclamation mark in the attached image), but didn't tell me about it in any way except the mysterious SIGABRT....

Subclassing UICollectionViewLayoutAttributes

I recently started playing with the awesome UICollectionView API, making reasonable progress, but have been stuck for almost all day with an issue I'm hoping someone can help me with:
I need to add some custom details to certain cells' attributes.
In order to do this, the right approach seem to be to subclass UICollectionViewLayoutAttributes and add the properties I need to my subclass. So far so good, except that when I return my LayoutAttributesSubclass, I always get the, somehow obscure, following error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: key cannot be nil'
Having tried to track this down for a while, I'm under the impression that the error is related to representedElementKind and representedElementCategory being nil in my subclass's instance. But those properties are read only, so I can't set those.
I've managed to somehow bypass the error by cheating, getting a regular UICollectionViewAttributes instance, then changing it to a LayoutAttributesSubclass using object_setClass, but this then raises a ton of other issues, plus seems rather shady to me.
In short, does anyone knows what the error above means, and how to correctly create/use UICollectionViewLayoutAttributes subclasses?
When setting up custom attributes, you need to subclass UICollectionViewLayoutAttributes and subclass UICollectionViewLayout and "declare" your custom attribute subclass class name by overriding +(Class)layoutAttributesClass in your UICollectionViewLayout class. The system calls this class method to see if there is a custom class to be supplied when you use the factory method for instantiating/dequeuing layout attribute objects.
#interface YourCustomCollectionViewAttributes : UICollectionViewLayoutAttributes
#property (nonatomic) UIEdgeInsets myCustomProperty
#end
#interface YourCustomCollectionViewLayout : UICollectionViewLayout
#end
#implementation YourCustomCollectionViewLayout
+ (Class)layoutAttributesClass
{
return [IRTableCollectionViewLayoutAttributes class];
}
#end
This is correct according to the documentation and should prevent the particular error you are having. Also when you implement custom iVars, be sure to implement an override for -(id)copyWithZone: or the UICollectionView will lose any custom values you have applied to your custom collection view object.

Resources