NSUInteger Equivalent for use in cocos2d-x project - objective-c

Is there any macro for NSUInteger implemented in cocos2d-x for use in visual c++. I need this to port a project from obj-c to c++.
And also , I have seen these variables written after the declaration of the variables in the header file :
#property (nonatomic, readonly) CGPoint stickPosition;
#property (nonatomic, readonly) float degrees;
#property (nonatomic, readonly) CGPoint velocity;
#property (nonatomic, assign) BOOL autoCenter;
#property (nonatomic, assign) BOOL isDPad;
#property (nonatomic, assign) BOOL hasDeadzone;
#property (nonatomic, assign) NSUInteger numberOfDirections;
#property (nonatomic, assign) float joystickRadius;
#property (nonatomic, assign) float thumbRadius;
#property (nonatomic, assign) float deadRadius;"
Is there use in writing these in any way in c++ and is it possible? If so How?

NSUInteger is just the biggest-sized unsigned integer. In C++, you can use
unsigned long // when building a 64-Bit OS app
unsinged int // when building a 32-Bit OS app (ie iOS, Android)
instead of it. Or, to make it convenient:
typedef unsigned long NSUInteger;
typedef unsigned int NSUInteger;
If you want to represent a property in C++, that doesn't really belong to this topic - it's not Cocoa-dependent but language-dependent, simply solvable by reading the topic on member variables of a C++ book. Here's how:
class Whatever {
public:
CGRect rect;
}
then access it like:
Whatever *instance = new Whatever();
CGRect r = instance->rect;

Related

Assigned to Readonly Property Objective-C

I am writing a unit test to test a method that updates a checklist. The checklist has these properties:
typedef NS_ENUM (NSUInteger, ChecklistStatus) { Pending, Completed };
#protocol IChecklistItem <NSObject>
#property (nonatomic, assign, readonly) NSInteger Id;
#property (nonatomic, copy, readonly) NSString *Description;
#property (nonatomic, assign, readonly)BOOL IsCompleted;
#property (nonatomic, assign, readwrite) ChecklistStatus Status;
#property (nonatomic, strong, readwrite) NSDate *CompletedDate;
#property (nonatomic, copy, readwrite) NSString *CompletedByUserId;
#property (nonatomic, assign, readonly) NSInteger RoleId;
#property (nonatomic, assign, readonly) NSInteger GroupId;
#property (nonatomic, strong, readonly) NSArray<IChecklistNote> *Notes;
- (void)sortNotes;
#end
However, in my unit test, as I am trying to validate,
checklistItem.Description = #"hello";, I get the error"Assignment to readonly property"
Why is this so?
heres the rest of my test method:
- (void)testUpdateChecklist {
NSString *testChecklistId = #"1";
NSString *testPatientDescription = #"Descriptive Description";
// What other properties do I need here?
XCTAssertNotNil(_service);
__block CCChecklistItem *checklistItem = nil;
SignalBlocker *blocker = [[SignalBlocker alloc] initWithExpectedSignalCount:1];
id delegate = OCMProtocolMock(#protocol(ChecklistServiceDelegate));
OCMExpect([delegate didCompleteUpdateChecklistItem:[OCMArg checkWithBlock:^BOOL(id obj) {
checklistItem = obj;
XCTAssertNotNil(checklistItem);
[blocker signal];
return true;
}]]);
[_service updateChecklistItem:checklistItem delegate:delegate];
[blocker waitWithTimeout:5.0f];
OCMVerifyAll(delegate);
NSString *originalDescription = checklistItem.Description;
checklistItem.Description = #"hello";
}
EDITED QUESTION:
So when I change the property from above to ReadWrite, I get this error in CChecklistItem
#interface CCChecklistItem ()
#property (nonatomic, assign, readwrite) NSInteger Id;
#property (nonatomic, copy, readwrite) NSString *Description;
#property (nonatomic, assign, readwrite) NSInteger RoleId;
#property (nonatomic, assign, readwrite) NSInteger GroupId;
#property (nonatomic, strong, readwrite) NSMutableArray<IChecklistNote> *Notes;
#end
`Illegal redeclaration of readwrite property in class extension 'CChecklistItem'
Your property is set to readonly as seen here:
#property (nonatomic, copy, readonly) NSString *Description;
Change it to:
#property (nonatomic, copy) NSString *Description;
or if you want to be consistent with the other properties (though overly explicit, IMO):
#property (nonatomic, copy, readwrite) NSString *Description;
Changing scope visibility only to satisfy tests is not encouraged. The easiest solution in your case would be to take advantage of wonderful KVO which Objective-C gives you.
Translated to your original question it would be something like:
[checklistItem setValue:#"hello" forKey:#"Description"]
No need to change access modifiers and your tests will be fine.
Your property is declared readonly in the protocol that the class CChecklistItem conforms. When that property is then synthersized it will create the backing variable and a getter method -(NSString *)description; but no setter method, since it is readonly. So redeclaring it as readwright in your anonymous category, that i'm guessing is declared in your test file to expose private methods to the test case, won't work since there still is no setter method for the property. Further more, even if you decide to try to make your own setter in the implementation of a category on your class you can't since there is no way to access the variable _description that is only exposed in the CChecklistItem.m file.
Depending on what you need to do with your test it might work to stub the getter - (NSString *)description; and return your #"hello" string when that method is called instead of trying to set the actual value to the backing variable.

Static Instance releasing properties prematurely with ARC

I have a static instance variable that is being used throughout my application. It has properties. These properties are used through my application and seem to work pretty well. However, sometimes the properties are released prematurely. What is odd is that the object that is pulling these properties keeps some and releases others. What would be a good way to insure that the properties of my object are not released prematurely.
Edit: It turns out that the issue was not premature releasing at all. It was a conversion issue. Thanks all for help.
#interface Game : NSObject
#property (nonatomic, strong) PFObject *gameObject;
//#property (nonatomic, strong) Concept *concept; // Will need to add Concept Object to GameObject once it's wrapper is done
#property (nonatomic, strong) User *initialPlayer;
#property (nonatomic, strong) User *invitedPlayer;
#property (nonatomic, strong) User *lastPlayedPlayer;
#property (nonatomic, strong) NSDate *lastPlayedDate;
#property (nonatomic, strong) NSDate *timeOutDate;
#property (nonatomic, assign) int timerTicks;
#property (nonatomic, assign) int currentRoundNumber;
#property (nonatomic, strong) User *winnerPlayer;
#property (nonatomic, assign) int initialPlayerPoints;
#property (nonatomic, assign) int invitedPlayerPoints;
#property (nonatomic, assign) int currentPlayerPoints;
#property (nonatomic, assign) GameStatus status;
#property (nonatomic, assign) int initialPlayerTimeouts;
#property (nonatomic, assign) int invitedPlayerTimeouts;
#property (nonatomic, assign) BOOL isInitialPlayer;
#property (nonatomic, strong) NSMutableDictionary *rounds;
#property (nonatomic, strong, readonly) Round *currentRound;
+(void)getActiveUserGameObjects:(PFUser *)user target:(id)target selector:(SEL)selector;
+(void)getYourTurnGameObjects:(PFUser *)user target:(id)target selector:(SEL)selector;
+(void)getTheirTurnGameObjects:(PFUser *)user target:(id)target selector:(SEL)selector;
+(void)getGameObjects:(PFUser *)user yourTurn:(id)yourTurn target:(id)target selector:(SEL)selector;
+(Game*)currentGame;
+(void)setCurrentGame:(Game*)currentGame;
..
//.m #implementation
..
static Game *sharedInstance = nil;
..
+(Game*)currentGame
{
return sharedInstance;
}
+(void)setCurrentGame:(Game*)currentGame
{
sharedInstance = currentGame;
}
...
#pragma mark - Player Setters and Getters
-(void)setInvitedPlayer:(User *)invitedPlayer
{
if (nil != invitedPlayer.userObject)
{
[self.gameObject setObject:invitedPlayer.userObject forKey:GAME_INVITED_PLAYER];
}
}
-(User*)invitedPlayer
{
NSObject *value = [self.gameObject objectForKey:GAME_INVITED_PLAYER];
if ([value isKindOfClass:[PFUser class]])
{
return [User userFromPFUser:(PFUser*)value];
}
return nil;
}
What would be a good way to insure that the properties of my object are not released prematurely.
Holding onto them by maintaining a strong reference. It is extremely unlikely that ARC is randomly releasing your data. Much more likely is that you are letting go of it when you don't mean to.
The first place I'd look is at your use of setCurrentGame:, making sure that you're not accidentally working on different Game objects at the same time in different parts of the program. First, make sure t
Your +get... methods are awkwardly named (a get prefix means a very specific thing in ObjC, and it's not what you're doing here). Havings class methods that take targets and actions like this seems a likely place to have trouble. It makes me wonder what's going on inside there.
Your conversion between two kinds of User objects is a little suspicious, and I'd make sure you're not accidentally dropping User or PFUser objects when you don't mean to.
Generally speaking, though, this question is over-vague. Are you winding up with dangling strong pointers? Are your strong pointers seeming to become nil? Is your game object itself becoming nil? How do you know when things are being "released?" Or do you mean that they're deallocating? Have you put a breakpoint in dealloc to see who had the last reference to the object?
Make sure your properties are (retain) type (same as strong, I think), and keep an instance refereed to in the app delegate (so that ARC sees the reference as valid for the entire execution of your program).

extern usage in Xcode4

I am getting the following error
Expected '{' before 'extern'
while using a third party header file and it has the following code
extern NSString *const kXXXError;
Is the usage related to Xcode version as currently i am using Xcode4
extern NSString *const kDeviceErrorDomain;
extern const NSInteger kDeviceErrorCodeUnknown;
#class DeviceInsightInternal;
#interface Device : NSObject {
DeviceInternal *_internal;
}
- (id)init;
- (NSString*)collect:(NSError**)error;
#property (nonatomic, assign) BOOL allowsJavascript;
#property (nonatomic, assign) BOOL privacyEnabled;
#end
You cannot declare a scalar property to have a any sort of retain,assign,copy semantic, it will result in a compile error. You should change:
#property(nonatomic, assign) BOOL allowsJavascript;
#property (nonatomic, assign) BOOL privacyEnabled;
to
#property(nonatomic) BOOL allowsJavascript;
#property (nonatomic) BOOL privacyEnabled;

Objective C: member variables and arrays

i have this variables (int and double-array)
.h-File
#interface MyCLass : NSObject
{
int myInt;
double paramStack[100];
}
#property (nonatomic, assign) int myInt;
//#property (nonatomic, assign) double paramStack; //<- ?
.m-File
#synthesise myInt;
//#synthesize paramStack; //<- ?
I want the int and the double-array-variable accessible from other classes via properties.
For the int-var. it looks fine, but the array throws errors at .m-file (#synthsize) and at h.file (#property (nonatomic, assign) double paramStack).
How can i define
"#property (nonatomic, assign) double paramStack;" as a double-array?
Thanks
Make the property with a pointer:
#property(nonatomic, assign) double *paramStack;
You can just use it like this:
NSLog(#"%f", self.paramStack[20]);
This is mainly because an array cannot be returned, but a pointer can. I.E. this getter would be impossible and that's why you cannot create an array property:
- (double[100])paramStack;

Obj-C selector not recognized on property?

I'm getting a NSObject doesNotRecognizeSelector error when trying to set a property and I'm not sure why.
The error occurs on the first line of setWithNSColor. I'm a bit confused how a property that's properly synthesized could be not recognized.
#interface ScopeColor : NSObject {
NSString *colorIntegerString;
float redColor;
float greenColor;
float blueColor;
NSString *name;
}
#property (readwrite, assign) NSString *colorIntegerString;
#property (readwrite, assign) float redColor;
#property (readwrite, assign) float greenColor;
#property (readwrite, assign) float blueColor;
#property (readwrite, assign) NSString *name;
-(void)setWithNSColor:(NSColor *)inColor
{
self.redColor=[inColor redComponent];
self.greenColor=[inColor greenComponent];
self.blueColor=[inColor blueComponent];
}
Are you sure it is your class and not NSColor that is raising the exception? If the NSColor object does not belong to the NSCalibratedRGBColorSpace or NSDeviceRGBColorSpace asking for redComponent, etc. will raise an exception.
Are you synthesizing redColor, greenColor, and blueColor somewhere outside of the included code? Also, primitive values (such as floats) don't need the assign keyword.