Is this undefined behaviour with Objective-C properties? - iphone

I had something like the following code in a project I recently worked on.
#interface DetailsViewController : UIViewContoller {
UIView* headerView_;
}
#property (nonatomic, retain) UIView* headerView;
#end
#implementation DetailsViewController
#synthesize headerView = undefinedVariableName_;
// ...
#end
undefinedVariableName_ was not defined anywhere in the project and was actually a much less obvious typo.
This compiled perfectly fine (no errors or warnings) and even ran fine on iOS 4. I did not catch this error until the program crashed on 3.1.3 firmware.
Does anyone know if the above behaviour is considered undefined? Is there a way to have the compiler catch such mistakes?

In the modern Objective-C runtime you don’t have to declare the ivars yourself, the compiler will create them for you at the point of #synthesize. If it crashed on the older iOS this version probably doesn’t support the modern runtime yet.

Related

non ARC and weak property- ios 5

I am using cocos2d v1.1.0-beta2b.
My project is NOT using ARC.
It used to work great but we have just tried changing now the minimal ios version for the project from 4.3 to 5.0 and we started to get compile time errors
"synthesize of weak properties is only allowed in ARC or GC mode"
it seems to be because of this(an example from cocos2d code)
// The delegate of the scroll layer object.
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_5_0
#property (nonatomic, weak) id<CCScrollLayerDelegate> delegate;
#else
#property (nonatomic, assign) id<CCScrollLayerDelegate> delegate;
#endif
What should i do to solve it?
Is it safe to simply remove this if from the code and leave just the line
#property (nonatomic, assign) id delegate; ?
(I am assuming it's ok as until today our code has used this anyway because until today we have targeted 4.3 and not 5)
__weak is an arc only feature and there is no official replacement
Try MAZeroingWeakRef by mike ash
(https://github.com/mikeash/MAZeroingWeakRef)
OR
PLWeakCompatibility by plausible labs
(https://github.com/plausiblelabs/PLWeakCompatibility)
--
You CAN fall back on assign and code will compile but as there is no automatic nilling of assign variables (as opposed To __weak vars) it changes semantics and may cause crashes down the road
Use unsafe_unretained instead of weak in your property declaration, and it should mostly work the same way.
unsafe_unretained is supported in both iOS 4.x and 5.0 and above, so it gives you backwards compatibility

AppDelegate.h errors - XCode

This is really weird. When coming back from school today and firing up my Mac Mini, I open up Xcode and I am presented with two errors in my AppDelegate.h file. One of these errors says Expected selector for Objective-C method and the other one is Expected method body. I googled these two errors and found nothing that could solve my case. I have tried restarting Xcode and as well as my computer. I have also tried "Cleaning" the project and still my problem is not resolved. What could this error be? Is it on my end? Or is this an Xcode bug? Thanks:
Code: AppDelegate.h
#import <UIKit/UIKit.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) UINavigationController *navigationController;
#end
Check Your main.m File for any extra or invisible Code:
//
// main.m
// Demo
//
// Created by Stackoverflow on 12/20/12.
//
//
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char *argv[])
{
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
What's in the corresponding .m file?
Are you #synthesizing accessors for your two properties? Failing to provide accessors (either synthesizing them or providing your own) seems like the most likely cause of the errors you've shown.
Have you implemented the usual app delegate methods? Off the top of my head, I'm not sure which if any app delegate methods are actually required, but your app isn't going to work very well and will produce some warnings if you don't have at least a -applicationDidFinishLaunching:withOptions:.
With Xcode newer than 5.0, Anyone confronting this issue can from the top menu\Product\Build.
It should resume right in the spot.

Duplicate interface declaration errors when I create MobileSubstrate tweaks

I have a problem about making Mobile Substrate tweaks for iOS 5.
Most tutorial about making Cydia tweaks have this step: "Download private framework headers".
So, I downloaded it from : https://github.com/kennytm/iphone-private-frameworks
Since the private-frameworks are dumped from iOS 3.x, some new methods and variables are not included.
Therefore, I added these variables to my Tweak.xm. And I imported the private-framework-headers too.
For example:
#import "/opt/theos/include/UIKit/UIKit2.h"
#import "/opt/theos/include/UIKit/UIKeyboardLayoutStar.h"
#interface UIKeyboardImpl : UIView
#property(assign, nonatomic) BOOL showsCandidateInline;
#property(assign, nonatomic) BOOL showsCandidateBar;
#end
However, when I compile the tweak, I got these errors:
Tweak.xm:45: error: duplicate interface declaration for class ‘UIKeyboardImpl’
Tweak.xm:45: error: redefinition of ‘struct UIKeyboardImpl’
Tweak.xm:45: error: trying to finish struct, but kicked out due to previous parse errors
How can I do to fix this problem ?
Should I edit the private framework headers of iOS 3 and add new variables from iOS 5?
Thanks a lot
Adding a category will fix it.
#interface UIKeyboardImpl (YourCategory)
#property(assign, nonatomic) BOOL showsCandidateInline;
#property(assign, nonatomic) BOOL showsCandidateBar;
#end

Header file declaration in iPhone

What is the difference between them:-
Approach 1:-
#interface EffortView : UIView {
}
#property (nonatomic, retain) UIView *homeView;
#end
Approach 2:-
#interface EffortView : UIView {
UIView *homeView;
}
#property (nonatomic, retain) UIView *homeView;
#end
I have synthesized the properties in both cases. Both of them works. I am using Xcode 4.0 on Mac 10.6.6
Please enlighten me.
Thank you All.
The first approach won't work on 32-bit Mac OS X runtimes because each property must have a corresponding instance variable. 64-bit and iOS runtimes automatically create the instance variable for you, so in that case, it is enough to use the second approach.
The bottom line is: if you are 100% sure that you won't ever target 32-bit Mac OS X systems and none of the components of your software will ever be used on that platform, you can safely omit the instance variables.

iPhone : Simple UITableViewController crash without console error nor debugging clue

I'm trying to build a simple TableView program struture.
It seems to work fine, but if I scroll the list to high or to low, the app crashes without any console error and the trace into the debugger does not help.
You can see it by yourself looking at the project I put at : http://shine.free.fr/tmp/myTestApp.zip
Can you help me :
to know what goes wrong
to know how I may find what goes wrong without having to ask each time. Usually I check my connection, lokk for compile errors, look into the console and try to debug, but there, nothing helps me.
Thank you for your help
The problem is that your ListController object is not retained when it is loaded from nib file, so it is not guaranteed that it will be valid after nib is loaded (and in fact it is not). To solve your problem add an outlet for ListController property and define retaining property for it. Here's FenetreListeController.h that fixes your problem:
#import <UIKit/UIKit.h>
#class ListeController;
#interface FenetreListeController : UIViewController {
IBOutlet ListeController* listController;
}
#property (nonatomic, retain) ListeController* listController;
#end
You will also need to set outlet connection in IB and synthesize property in .m file
For more information about how objects are loaded from xib files check "The Nib Object Life Cycle" section from "Resource Programming Guide"