Design issue in Iphone Dev - Generic implementation for Game Bonuses - iphone

So, I thought consulting you guys about my design, cause I sense there might be a better way of doing it.
I need to implement game bonuses mechanism in my app.
Currently there are 9 bonuses available, each one is based of different param of the MainGame Object.
What I had in mind was at app startup to initialize 9 objects of GameBonus while each one will have different SEL (shouldBonus) which will be responsible for checking if the bonus is valid.
So, every end of game I will just run over the bonuses array and call the isBonusValid() function with the MainGame object(which is different after every game).
How's that sound ?
The only issue I have currently, is that I need to make sure that if some bonuses are accepted some other won't (inner stuff)... any advice how to do that and still maintain generic implementation ?
#interface GameBonus : NSObject {
int bonusId;
NSString* name;
NSString* description;
UIImage* img;
SEL shouldBonus;
}
#implementation GameBonus
-(BOOL) isBonusValid(MainGame*)mainGame
{
[self shouldBonus:mainGame];
}
#end

Sounds ok, the only change I would consider is perhaps removing a bonus from the array should it be acepted. That way it is not checked in the future. This would also work for bonus that for other reason should no longer be available.

Whether or not the player can obtain a particular bonus according to the rules of your game isn't something the individual bonuses would know about. This is something the game itself would know. For example, you may have one game that allows bonuses A and B together, but another game that wouldn't.
So the logic to grant or deny a bonus should be in the MainGame object. I would organize it so that GameBonus is a plain bit bucket class and the logic is all in the MainGame. MainGame would be a superclass of any other custom games that might want to override the bonus logic.
A starting point:
typedef enum {
BonusTypeA, BonusTypeB, BonusTypeC
} BonusId;
#interface GameBonus : NSObject {
BonusId bonusId;
NSString *name;
NSString *description;
UIImage *img;
}
#property (nonatomic,assign) BonusId bonusId;
#property (nonatomic,retain) NSString *name;
#property (nonatomic,retain) NSString *description;
#property (nonatomic,retain) NSString *img;
#end
#interface MainGame : NSObject {
NSMutableSet *activeBonuses;
}
-(BOOL) tryToSetBonus:(BonusId)bonus; // tries to set, returns YES if successful.
-(BOOL) isBonusValid:(BonusId)bonus; // has no side effect, just check validity.
#end

Related

GLKQuaternion in Swift

Trying to re-write Objective-C code written for the Myo I came across the GLKQuaternion class which is a union and doesn't seem to be supported in Swift (Using GLKMath from GLKit in Swift).
What I am looking for is a workaround to get this to work. Maybe a hybrid application with Math being done in ObjectiveC so I can access those properties. What are my options?
Ended up using a bridge file and creating a small wrapper layer to allow me access to the data I wanted from Swift. I called it a polyfill, similar to what we had on the web to enhance capabilities of old browsers.
The code is available on github. Particularly this commit will be of interest as it shows what needs to be done.
Polyfill.h
#interface OrientationData: NSObject
#property (nonatomic) TLMAngle *pitch;
#property (nonatomic) TLMAngle *yaw;
#property (nonatomic) TLMAngle *roll;
#end
#implementation OrientationData
#end
#interface GLKitPolyfill: NSObject
+ (OrientationData *) getOrientation:(TLMOrientationEvent *)orientationEvent;
#end
Polyfill.m
+ (OrientationData *) getOrientation:(TLMOrientationEvent *)orientationEvent {
TLMEulerAngles *angles = [TLMEulerAngles anglesWithQuaternion:orientationEvent.quaternion];
OrientationData *result = [OrientationData new];
result.pitch = angles.pitch;
result.yaw = angles.yaw;
result.roll = angles.roll;
return result;
}
ViewController.swift
let angles = GLKitPolyfill.getOrientation(orientationEvent)
let pitch = CGFloat(angles.pitch.radians)
let yaw = CGFloat(angles.yaw.radians)
let roll = CGFloat(angles.roll.radians)
Apples Mix & Match guide gives good tips on how to make Obj-C work with Swift and vice-versa.

Dropbox Datastore Class Structure on iOS

There is an iOS sample project for the Dropbox Datastore API available here: https://www.dropbox.com/developers/datastore/sdks/ios
Inside that project's main class (TasksController.m), it has some API-related properties:
#property (nonatomic, readonly) DBAccountManager *accountManager;
#property (nonatomic, readonly) DBAccount *account;
#property (nonatomic, retain) DBDatastore *store;
...as well as some private methods:
- (DBAccountManager *)accountManager {
return [DBAccountManager sharedManager];
}
- (DBAccount *)account {
return self.accountManager.linkedAccount;
}
- (DBDatastore *)store {
if (!_store && self.account) {
_store = [DBDatastore openDefaultStoreForAccount:self.account error:nil];
}
return _store;
}
There aren't any code comments describing these. Why is this class structured this way? Do all classes working with the API need to use a similar structure?
For example, can I just declare DBAccount *account in viewDidLoad or should I be using the read-only property somehow?
I guess it depends on what scope you want. If you don't need the DBAcccount outside of viewDidLoad, then feel free to just declare it there. For the DBDatastore, however, you need to make sure it stays in scope for the lifetime of your app (or at least as long as you care about it syncing with Dropbox). As soon as it goes out of scope, it will also stop syncing changes to and from the server.

Class not setting as expected

Icon is set as #property (nonatomic, retain) AHGridIcon *icon;
Usually i just do:
-(void)setIcon:(AHGridIcon *)iconLocal {
icon = iconLocal;
}
But i read a guide to getters setters and properties online which has lead me to believe that instead, this is right:
-(void)setIcon:(AHGridIcon *)iconLocal {
if (iconLocal != self.icon)
{
NSLog(#"local: %#", iconLocal);
NSLog(#"self.icon 1: %#", self.icon);
[iconLocal retain];
[icon release];
icon = iconLocal;
NSLog(#"self.icon 2: %#", self.icon);
}
}
The problem is, the original icon is staying put, it's not being replaced with the new icon. What am i doing wrong? Should i just revert to the usual way i do it?
You should use '#synthesize' unless you really need custom setter behavior.
like I posted in my comment:
the best way is to use #synthesize which will create a getter and a setter to with respect to the properties you wrote in your property (nonatomic, retain) => not threadsafe but fast getter and setter and a retaining (and also releasing) setter. If you dont need sophisticating stuff to do in your setter then you should not override the setter.
.h:
#property (nonatomic, retain) AHGridIcon *icon;
.m:
#implementation Something
#synthesize icon;
...
#end
The code you posted in your setter is nearly the same as the compiler would produce when only using synthesize.
Your usual way is not really nice because in your header is defined (in your property) that the setter is retaining but in your implementation you are overriding that correct setter which doesn't retain. It is nearly the same as the compiler would produce with an (nonatomic, assign) property.
But if you want to override your setter then it should look like the same as you wrote. For me it is working fine.
first retaining the new object
then releasing the old one
then assigning the local pointer to your new object
you can even omit your if but then it is really important that you first retain the new and then release the old objects (like you did - just want to mention that).
For solving your problem with an overriten setter: Your setter looks ok in my eyes. Have you also overriten the getter? If yes then post it here (you use it by calling self.icon in your log-call).
I've done a small test-program
#synthesize str;
- (void)setStr:(NSString *)localStr
{
if(str != localStr)
{
NSLog(#"old : %#", self.str);
NSLog(#"new1: %#", localStr);
[localStr retain];
[str release];
str = localStr;
NSLog(#"new2: %#", self.str);
}
}
and the output is fine:
old : (null)
new1: Hello
new2: Hello
old : Hello
new1: World
new2: World

Problem with singleton

I want to make a singleton containing information "title, comments, Two picture" and it saves all the information in an array
I want to do is these objects in my application I use it All The Time
#interface CarteManager : NSObject {
NSMutableArray *carteMan ;
}
#property(nonatomic,retain) NSMutableArray *carteMan;
+(CarteManager*)sharedInstance;
-(void)ajouttitre:(NSString*)txt;
-(void)ajoutcom:(NSString*)com;
-(void)ajoutimage1:(UIImage*)img;
-(void)ajoutimage2:(UIImage*)img;
#end
In order to create a Singleton you will need a static instance.
#implementation CarteManager
static CarteManager *_carteManager = nil;
+(CarteManager*)sharedInstance {
if (!_carteManager) {
_carteManager = [[CarteManager alloc] init];
}
return _carteManager;
}
// your other codes
#end
And before creating a Singleton, make sure that you really need a Singleton. Please pay special attention to Singleton: How should it be used.
You didn't state your problem. If it's how to make the object a singleton, you can find several possible implementations in the question What does your Objective-C singleton look like?.

Set a variable in a different class

I am trying to create an application were 2 classes share a variable. Just to keep the code looking a little bit cleaner I created a 3rd class. This "third class" sole job is to house this variable.
In class 3 I put a "get" and "set" method.
SharedURL.H (Class 3)
#interface SharedURL : NSObject {
NSString *theURL;
}
-(NSString *)getTheURL;
-(void)setTheURL:(NSString *)blah;
#property (readwrite, copy) NSString *theURL;
#end
Implementation:
#import "SharedURL.h"
#implementation SharedURL
#synthesize theURL;
-(NSString *)getTheURL;
{
return theURL;
}
-(void)setTheURL:(NSString *)blah;
{
theURL=blah;
}
#end
In classes 1 and 2:
I Import the class header
I set up the instance variable like so
SharedURL *XMLURL;
I define the property like so
#property (readwrite, assign) SharedURL *XMLURL;
Then in the implementation I set the set method like this
[XMLURL setTheURL:#"http://localhost:8888/xml/MyXMLFile.xml"];
However whenever I implement the fallowing code the getter method returns nil.
NSLog(#" the url is %#", [XMLURL getTheURL]);
How can I get this to actually save the variable that I imput and then return it. I'm looking at some sample code and i cannot find my error it looks to me like I am doing it perfectly fine I think I am overlooking something stupid.
If I understand this right you are calling class 3 from either class 1 or 2 (lets say 1) and set the URL then your go to class 2 and and only ask for the URL, right?
I think your problem is that you are calling something that is independent for each object. I think you can fix this by instead of saying -(NSString *)getTheURL and -(void)setTheURL you need to change it to +(NSString *)getTheURL and +(void)setTheURL (in both the .h and .m files) making it not variable dependent.