define a cclayer in .h file - objective-c

In my I've defined a CCLayer like this:
#interface MyLayer : CCLayer {
CCLayer * referenceLayer;
}
How should I declare it to use it in +(CCScene *) scene ?
Like this ?
#property (nonatomic, retain) CCLayer *referenceLayer;

Since + (id)scene is a class method, you cannot access an ivar/property from within it. One possible solution is having a static variable in your layer.m file, like in the following snippet:
static CCScene* _scene = nil;
+ (id)scene {
if (_scene == nil) {
_scene = [[CCScene node] retain];
//-- further scene initializaion
}
return _scene;
}
This simple approach has a drawback: you can only have one such layer around.

Related

set Property method called tens of thousands of times when I use a custom UI view in react native iOS

I created a custom UI view under this facebook tutorial https://facebook.github.io/react-native/docs/native-components-ios.html#content
MyCustomView.h
#interface MyCustomView : UIView
#property (nonatomic, copy) NSString *test;
#end
MyCustomView.m (init method is omitted)
- (void)setTest:(NSString *)test
{
self.test = test;
}
And I expose this property to JS in MyCustomViewManager
MyCustomViewManager.h
#interface ReactVideoViewManager : RCTViewManager
#end
MyCustomViewManager.m
RCT_EXPORT_MODULE()
- (UIView *)view
{
MyCustomView *videoView = [[MyCustomView alloc] init];
return;
}
RCT_EXPORT_VIEW_PROPERTY(test, BOOL);
I used this view in JS as follows:
<MyCustomView style={styles.row} test={true}></MyCustomView>
However, when the MyCustomView object initialized, the setTest method called tens of thousands of times and crashed at last.
Did anyone know the reason?
a working solution might be:
- (void)setTest:(NSString *)test
{
_test = test;
}

Infinite loop when redefining the setter of a property

In Cocoa Programming for Mac OS X (Hillegass), there is a class in chapter 19:
#interface BigLetterView : NSView {
NSColor *bgColor;
}
#property (strong) NSColor *bgColor;
#end
The accessor is defined like this:
- (void)setBgColor:(NSColor *)c {
bgColor = c;
[self setNeedsDisplay:YES]; }
This looked correct to me, but it creates an infinite loop: bgColor = c calls setBgColor:c
Is this code correct ?
How to redefine a setter? What do I need to compile this code ?
In modern Xcode/llvm you can clean up your code a bit.
Namely you dont need to declare neither a member variable as backing variable, nor a synthesize statement.
If you dont declase synthesize your self, a property foo will have the backing variable _foo
So this code should work
#interface BigLetterView : NSView
#property (nonatomic, strong) NSColor *bgColor;
#end
#implementation BigLetterView
#synthesize bgColor = _bgColor;
- (void)setBgColor:(NSColor *)c
{
_bgColor = c;
[self setNeedsDisplay:YES];
}
#end
Do it this way which looks cleaner and better to understand.
#interface BigLetterView : NSView
#property (strong) NSColor *bgColor;
#end
#implementation BigLetterView
- (void)setBgColor:(NSColor *)c {
_bgColor = c;
[self setNeedsDisplay:YES];
}
Also, make a simple rule to always use the property with self in any other places you use it. It is just a better rule for making you understand things clearly.

Access Class without initializing

I want to create a class in objective-c with its methods, so that for accessing the data I don't want to instantiate the class. how can I do it?
Either you can use singleton, or if you are planning to use only static methods, you can just add it in the class and use it directly with class name.
Create methods as static,
+(void)method;
then use it as,
[MyClass method];
This is helpful only if you are creating some utility classes which has only some utility method like processing an image or so. If you need to have property variables, you will need singleton.
For eg:-
Go to new file and create MySingleton class which will create MySingleton.h and MySingleton.m files.
In .h file,
#interface MySingleton : NSObject
{
UIViewController *myview;
}
#property (nonatomic, retain) UIViewController *myview;
+(MySingleton *)sharedSingleton;
In .m file,
+ (MySingleton*)sharedSingleton {
static MySingleton* _one = nil;
#synchronized( self ) {
if( _one == nil ) {
_one = [[ MySingleton alloc ] init ];
}
}
return _one;
}
- (UIViewController *)myview {
if (!myview) {
self.myview = [[[UIViewController alloc] init] autorelease]; //you can skip this, but in that case you need to allocate and initialize the first time you are using this.
}
return myview;
}
Then use it as,
[[MySingleton sharedSingleton] myview] anywhere in your project. Remember to import MySingleton.h though. Similarly you can create any object in singleton and use it. Just implement the getter or setter method accordingly.
One thing you have to be careful is that the object created in a singleton has only a single memory space allocated and hence it is the same object whenever you are using anywhere in your project. The above code will not create multiple copies of myview object in the class. So whenever you are modifying a property of myview that will be reflected everywhere. Use this approach only if it is absolutely needed and you need to have access to a single object from all over the project. Normally we use this only for situations like storing a sessionID which needs to be accessed from different classes etc..
You may use singleton pattern, check this question.
Like this:
+(MySingleton *)sharedInstance {
static dispatch_once_t pred;
static MySingleton *shared = nil;
dispatch_once(&pred, ^{
shared = [[MySingleton alloc] init];
shared.someIvar = #"blah";
});
return shared;
}
Or if you want to just access methods, you may use factory methods (those with +, not with -)
#interface MyClass
#property (nonatomic, assign) NSInteger value;
+ (void) factoryMethod;
- (void) instanceMethod;
...
// then in code
[MyClass factoryMethod]; // ok
[[MyClass sharedInstance] instanceMethod]; // ok
[MyClass sharedInstance].value = 5; // ok
UPDATE:
You may add a property to appDelegate
// in your app delegate.h
#property (nonatomic, retain) UIViewController* view;
// in your app delegate.m
#synthesize view;
and get appDelegate from almost any place like:
myapp_AppDelegate* appDelegate = [[UIApplication sharedApplicaton] delegate];
appDelegate.view = ...; // set that property and use it anywhere like this
Note, that you'll need to #import your UIViewController subclass and your appDelegate.h to make autocomplete work and sometimes avoid warnings.
// someFile.m
#import "appDelegate.h"
#import "myViewController.h"
...
myapp_AppDelegate* appDelegate = [[UIApplication sharedApplicaton] delegate];
appDelegate.view.myLabel.text = #"label text";

ObjC: Can't access ivar of custom class from outside

I'm attempting to make a basic game in cocos2d, and I've gotten to the point where I'm attempting to scroll the background depending on the hero sprite's position. The hero's created alongside the controls in a class called GameplayLayer, and all works fine for non-scrolling backgrounds.
The book I'm following has given me some sample code to get the scrolling working based on when my character passes the half-way point, which seems perfect, but it's not executing, I believe this is because it's creating another instance of my Hero class, and there's no link between that instance, and the one that's displayed onscreen.
I'm assuming that I can fix this by making the working instance of my hero accessible from within the background scrolling class (which is called YrrasCoastBackgroundLayer), but I'm having a brain-block and can't get it to see it. I've tried a #property, but Xcode just won't see it in the other class.
Here's the .h file from the GameplayLayer class, where the hero is created / hooked up to controls, etc.:
#import "CCLayer.h"
#import "cocos2d.h"
#import "CommonProtocols.h"
#import "SneakyJoystick.h"
#import "SneakyButton.h"
#import "SneakyJoystickSkinnedBase.h"
#import "SneakyButtonSkinnedBase.h"
#import "HeroMale.h"
#import "GameCharacter.h"
#interface GameplayLayer : CCLayer <GameplayLayerDelegate> {
CCSprite *playerSprite;
SneakyJoystick *playerJoystick;
SneakyButton *jumpButton;
CCSpriteBatchNode *sceneSpriteBatchNode;
HeroMale *heroMale;
}
#property (nonatomic, readonly) SneakyJoystick *playerJoystick;
#property (nonatomic, readonly) SneakyButton *jumpButton;
#property (nonatomic, assign) HeroMale *heroMale;
#end
The YrrasCoastBackgroundLayer.h file definitely imports GameplayLayer.h, and here's the contents of the method in YrrasCoastBackgroundLayer.m file which I want to be able to access that *heroMale ivar:
- (void) adjustLayer {
float heroXPosition = heroMale.position.x;
CGSize screenSize = [[CCDirector sharedDirector] winSize];
float halfOfTheScreen = screenSize.width / 2.0f;
CGSize levelSize = [[GameManager sharedGameManager] getDimensionsOfCurrentScene];
if ((heroXPosition > halfOfTheScreen) && (heroXPosition < (levelSize.width - halfOfTheScreen))) {
float newXPosition = halfOfTheScreen - heroXPosition;
[self setPosition:ccp(newXPosition, self.position.y)];
}
}
I'm getting an Xcode error on the float heroXPosition = heroMale.position.x line, stating that heroMale is an undeclared identifier. How can I make it usable here, and will that even solve the problem?
UPDATE:
Here's the .h file for YrrasCoastBackgroundLayer:
#import "CCLayer.h"
#import "cocos2d.h"
#import "HeroMale.h"
#import "GameplayLayer.h"
#interface YrrasCoastBackgroundLayer : CCLayer {
// Web Tutorial
CCParallaxNode *backgroundNode;
CCSprite *backgroundImage;
// Book Tutorial
CCSpriteBatchNode *sceneSpriteBatchNode;
CCParallaxNode *parallaxNode;
}
#property (nonatomic, assign) GameplayLayer *gameplayLayer;
#end
And here's the YrrasCoastBackgroundLayer.m:
#import "YrrasCoastBackgroundLayer.h"
#implementation YrrasCoastBackgroundLayer
#synthesize gameplayLayer;
- (id) init {
self = [super init];
if (self != nil) {
// Web Tutorial
backgroundNode = [CCParallaxNode node];
[self addChild:backgroundNode z: -1];
backgroundImage = [CCSprite spriteWithFile:#"yrras-coast-ipad-hd.png"];
CGPoint dustSpeed = ccp(0.1, 0.1);
[backgroundNode addChild:backgroundImage z:0 parallaxRatio:dustSpeed positionOffset:ccp(1024, [[CCDirector sharedDirector]winSize].height / 2)];
self.gameplayLayer = gameplayLayer;
[self scheduleUpdate];
}
return self;
}
- (void)update:(ccTime)deltaTime {
// Web Tutorial
// CGPoint backgroundScrollVel = ccp(-1000, 0);
// backgroundNode.position = ccpAdd(backgroundNode.position, ccpMult(backgroundScrollVel, deltaTime));
CCArray *listOfGameObjects = [sceneSpriteBatchNode children];
for (GameCharacter *tempChar in listOfGameObjects) {
[tempChar updateStateWithDeltaTime:deltaTime andListOfGameObjects:listOfGameObjects];
}
[self adjustLayer];
}
// Book Tutorial
- (void) adjustLayer {
float heroXPosition = gameplayLayer.heroMale.position.x;
CCLOG(#"heroXPosition is %f", gameplayLayer.heroMale.position.x);
CGSize screenSize = [[CCDirector sharedDirector] winSize];
float halfOfTheScreen = screenSize.width / 2.0f;
CGSize levelSize = [[GameManager sharedGameManager] getDimensionsOfCurrentScene];
if ((heroXPosition > halfOfTheScreen) && (heroXPosition < (levelSize.width - halfOfTheScreen))) {
float newXPosition = halfOfTheScreen - heroXPosition;
[self setPosition:ccp(newXPosition, self.position.y)];
}
}
#end
You need an instance of your GameplayLayer to be passed to the YrrasCoastBackgroundLayer layer.
You can declare an assignable property in YrrasCoastBackgroundLayer :
#property (nonatomic, assign) GameplayLayer gamplayLayer;
and when you initialize your YrrasCoastBackgroundLayer pass the instance of the game play layer and in your adjustLayer method do :
float heroXPosition = gameplayLayer.heroMale.position.x;
EDIT :
assuming this is how you create your scene :
MyScene *scene = [MyScene scene];
gameplayLayer = [[GameplayLayer alloc] init];
bgLayer = [[YrrasCoastBackgroundLayer alloc] init];
bgLayer.gameplayLayer = gameplayLayer; // This is where you assign the gameplay Layer to the background layer;
[scene addChild:bgLayer];
[scene addChild:gameplayLayer];
// Release both layers if you dont need them anymore here (scene addChild retains them when they are added)
[gameplayLayer release];
[bgLayer release];
I am not sure how can you access heroMale as it is not member of YrrasCoastBackgroundLayer but of GameplayLayer. You would have to have object/instance of class GameplayLayer in order for you to access heroMale. That is the reason why you are getting undeclared identifier (heroMale is not property of class YrrasCoastBackgroundLayer).
Kind regards,
Bo

setting new properties in category interface/implementation

Ok, so I have this, but it wont work:
#interface UILabel (touches)
#property (nonatomic) BOOL isMethodStep;
#end
#implementation UILabel (touches)
-(BOOL)isMethodStep {
return self.isMethodStep;
}
-(void)setIsMethodStep:(BOOL)boolean {
self.isMethodStep = boolean;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if(self.isMethodStep){
// set all labels to normal font:
UIFont *toSet = (self.font == [UIFont fontWithName:#"Helvetica" size:16]) ? [UIFont fontWithName:#"Helvetica-Bold" size:16] : [UIFont fontWithName:#"Helvetica" size:16];
id superView = self.superview;
for(id theView in [(UIView *)superView subviews])
if([theView isKindOfClass:[UILabel class]])
[(UILabel *)theView setFont:[UIFont fontWithName:#"Helvetica" size:16]];
self.font = toSet;
}
}
#end
If I take out the getter and setter methods then it doesn't work it tells me I need to create some getter and setter methods (or use #synthesize - but putting #synthesize in the #implementation throws an error too). But with the getter and setter methods I get an EXC_BAD_ACCESS and a crash. Any ideas? Thanks
Tom
It is not possible to add members and properties to an existing class via a category — only methods.
https://developer.apple.com/library/content/documentation/General/Conceptual/DevPedia-CocoaCore/Category.html
One possible workaround is to write "setter/getter-like" methods, that uses a singleton to save the variables, that would had been the member.
-(void)setMember:(MyObject *)someObject
{
NSMutableDictionary *dict = [MySingleton sharedRegistry];
[dict setObject:someObject forKey:self];
}
-(MyObject *)member
{
NSMutableDictionary *dict = [MySingleton sharedRegistry];
return [dict objectforKey:self];
}
or — of course — write a custom class, that inherits from UILabel
Note that nowadays an associated object can be injected during runtime. The Objective C Programming Language: Associative References
Checked all answers and did not find the most common solution:
#import <objc/runtime.h>
static void const *key;
#interface ClassName (CategoryName)
#property (nonatomic) BOOL myProperty;
#end
#implementation ClassName (CategoryName)
- (BOOL)myProperty {
return [objc_getAssociatedObject(self, key) boolValue];
}
- (void)setMyProperty:(BOOL)value {
objc_setAssociatedObject(self, key, #(value), OBJC_ASSOCIATION_RETAIN);
}
#end
swift:
private struct AssociatedKeys {
static var keyName = "keyName"
}
extension Foo {
var bar: Any! {
get {
return objc_getAssociatedObject(self, &AssociatedKeys.keyName)
}
set {
objc_setAssociatedObject(self, &AssociatedKeys.keyName , newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
}
There is actually a way, which may not be ideal, but does work.
For it to work, you will need to create a category for a class X and can only be used on subclasses of the same X (e.g. category UIView (Background) can be used with class MyView : UIView, but not directly with UIView)
// UIView+Background.h
#interface UIView (Background)
#property (strong, nonatomic) NSString *hexColor;
- (void)someMethodThatUsesHexColor;
#end
// UIView+Background.h
#implementation UIView (Background)
#dynamic hexColor; // Must be declared as dynamic
- (void)someMethodThatUsesHexColor {
NSLog(#"Color %#", self.hexColor);
}
#end
Then
// MyView.m
#import "UIView+Background.h"
#interface MyView : UIView
#property (strong, nonatomic) NSString *hexColor;
#end
#implementation MyView ()
- (void)viewDidLoad {
[super viewDidLoad];
[self setHexColor:#"#BABACA"];
[self someMethodThatUsesHexColor];
}
#end
Using this method, you will need to "redeclare" your properties, but after that, you can do all of its manipulation inside your category.
You could inject an associated object during runtime.
#import <objc/runtime.h>
#interface UIView (Private)
#property (nonatomic, assign) CGPoint initialTouchPoint;
#property (nonatomic, strong) UIWindow *alertWindow;
#end
#implementation UIView (Private)
#dynamic initialTouchPoint, alertWindow;
- (CGPoint)initialTouchPoint {
return CGPointFromString(objc_getAssociatedObject(self, #selector(initialTouchPoint)));
}
- (void)setInitialTouchPoint:(CGPoint)initialTouchPoint {
objc_setAssociatedObject(self, #selector(initialTouchPoint), NSStringFromCGPoint(initialTouchPoint), OBJC_ASSOCIATION_RETAIN);
}
- (void)setAlertWindow:(UIWindow *)alertWindow {
objc_setAssociatedObject(self, #selector(alertWindow), alertWindow, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIWindow *)alertWindow {
return objc_getAssociatedObject(self, #selector(alertWindow));
}
#end
EDIT: Warning: This property would have a unique value for all the instances of the class.
This worked for me, but only because I had only one instance of this class in my app.
#import <AVFoundation/AVFoundation.h>
#interface AVAudioPlayer (AstroAVAudioPlayer)
#property (nonatomic) BOOL redPilot;
#end
#import "AVAudioPlayer+AstroAVAudioPlayer.h"
#implementation AVAudioPlayer (AstroAVAudioPlayer)
BOOL _redPilot;
-(void) setRedPilot:(BOOL)redPilot
{
_redPilot = redPilot;
}
-(BOOL) redPilot
{
return _redPilot;
}
#end
A solution that I found to this was to just give each object that you want flagged a unique tag.
I made a UILabel category to add custom fonts to all my labels but on some i wanted them to be bold so i did this ->
- (void) layoutSubviews {
[super layoutSubviews];
[self addCustomFont];
}
- (void) addCustomFont {
if (self.tag == 22) {
[self setFont:[UIFont fontWithName:SEGOE_BOLD size:self.font.pointSize]];
}else{
[self setFont:[UIFont fontWithName:SEGOE_LIGHT size:self.font.pointSize]];
}
}
It seems as if since Xcode 7 (7.0.1, 7A1001), properties are supported in categories. I noticed that Xcode generates categories now for Core Data subclasses.
For example, I got the files:
Location+CoreDataProperties.h
#import "Location.h"
NS_ASSUME_NONNULL_BEGIN
#interface Location (CoreDataProperties)
#property (nullable, nonatomic, retain) NSNumber *altitude;
#property (nullable, nonatomic, retain) NSNumber *latitude;
#property (nullable, nonatomic, retain) NSNumber *longitude;
#end
NS_ASSUME_NONNULL_END
Location+CoreDataProperties.m
#import "Location+CoreDataProperties.h"
#implementation Location (CoreDataProperties)
#dynamic altitude;
#dynamic latitude;
#dynamic longitude;
#end
So looks like properties in categories might work now. I haven't tested on non-Core Data classes.
What I've noticed is that they do include the category file back into the original class:
Location.h
#interface Location : NSManagedObject
#end
#import "Location+CoreDataProperties.h"
This allows the original class to edit the properties specified by the category.