Objective-C:How to Converting int to id for selector's object? - objective-c

I received a return value from SQLite fetch
int primaryKey = sqlite3_column_int(statement, 0);
and I'm going to use it as a selector's object:
[[ABC alloc] performSelector:#selector(abcWithAAA:) withObject:[NSNumber numberWithInt:primaryKey]];
the NSLog result for primarykey is a number 4:
NSLog(#"primaryKey:%i",primaryKey);
4
but the NSLog result for [NSNumber numberWithInt:primaryKey] is 131628896.
why? and how do i convert the int value correctly?
Thanks!

I solved the problem using an adapter method that does the cast for the withObject method.
My problem was that I wanted to use a typedef enum and pass it as value to the withObject.
I wanted to call this method using the performSelect message:
-(void) requestInfosAndPersistByMonsterType:(MonsterTypes)monsterType {
}
As you see it request a MonsterTypes typedef defined like this:
typedef enum
{
MonsterTypeIWerwolf = 0,
MonsterTypeITempler = 1,
MonsterTypeIUndefined,
} MonsterTypes;
Actually to be able to call the method above I build this adapter that calls it then:
-(void)monsterTypeFromObject:(id)_monsterType {
if ([_monsterType respondsToSelector:#selector(intValue)]) {
int _t = [_monsterType intValue];
switch (_t) {
case MonsterTypeIWerwolf:
_t = MonsterTypeIWerwolf;
break;
case MonsterTypeITempler:
_t = MonsterTypeITempler;
break;
default:
_t = MonsterTypeIUndefined;
break;
}
[self requestInfosAndPersistByMonsterType:_t];
}
}
It is used this way:
[self performSelector:#selector(monsterTypeFromObject:) withObject:[NSNumber numberWithUnsignedInt:monsterType] afterDelay:5.0f];
You can find it explained in more detail here:
http://kerkermeister.net/objective-c-adapter-from-nsinteger-to-id-when-using-performselector-withobject/

When you log [NSNumber numberWithInt:primaryKey], you're logging the address of an NSNumber object. If you want to see what's inside it should be [[NSNumber numberWithInt:primaryKey] intValue].
In other words, there's nothing in that to suggest your conversion is a problem.

[NSNumber numberWithInt:primaryKey] is object.
use %# for objects.
NSLog(#"%#", [NSNumber numberWithInt:primaryKey]);

131628896 is the memory address of the NSNumber object.
use:
- (void)abcWithAAA: (NSNumber *)number {
int primaryKey = [number intValue];
NSLog("%i", primaryKey);
}

Related

how to test object equality using xcttest?

My method returns a NSNumber* and I want to unit test this method. Since my actual return value is NSNumber*, I create a new expected value of NSNumber*, but it fails. Here is the code:
NSNumber *cRating = [movie getRating:ratingDictionary ratingType:criticRating];
XCTAssertEqualObjects(cRating, [[NSNumber alloc]initWithInt:70], #"");
The error is:
[SFModelTest testGetCriticRatingMethod] failed: ((cRating) equal to ([[NSNumber alloc]initWithInt:70])) failed: ("70") is not equal to ("70")
Since it is saying "70" is not equal to "70", I am guessing it has to do with alloc init. Some pointer stuff that is not equal. Can somebody please help? Thank you.
Edit for comment: adding getRating method
- (NSNumber *)getRating:(NSDictionary *)movieDic ratingType:(enum RatingsEnum) rating{
NSNumber *result = 0;
NSNumber *ratingNum = 0;
switch (rating) {
case userRating:
{
ratingNum = [movieDic objectForKey:#"audience_score"];
break;
}
case criticRating:
{
ratingNum = [movieDic objectForKey:#"critics_score"];
break;
}
default:
break;
}
if(ratingNum && ratingNum > 0)
{
result = ratingNum;
}
return result;
}
The method returns NSNumber. And my test is:
NSNumber *cRating = [movie getRating:ratingDictionary ratingType:criticRating];
XCTAssertEqualObjects(cRating, [[NSNumber alloc]initWithInt:70], #"");
When I do a class NSLOG, it returns __NSCFConstantString. I am confused now.
I think that you're doing something wrong because XCTAssertEqualObjects(#(1), #(1), #"Not equal."); or XCTAssertEqualObjects([NSNumber numberWithInt:1], [NSNumber numberWithInt:1], #"Not equal."); if you are not familiar with literals, will pass.
You should checkout the getRating:ratingType: method to see which type of object it returns.
Try adding a breakpoint just before that XCTAssertEqualObjects and inspect the cRating instance.

iOS adding object to an array from another array base on 3rd array value

EDIT: All array used in my project are NSMutableArray class
An overview of what I want to do is from my selectClueView, user can select a number from 3-10. Which represent the number of clue they will play. It will then generate list of random number between 0 and the objectArray.count and add the NSNumber into another array known as dataArray. Everything is working fine including prepareForSegue which transfer SelectClueViewController.dataArray to GamePageViewController.clueToSelect
However, I am stuck with loading data into the new array ds, from an array that hold all the object allDataObject. I am fairly new to iOS and because I had a working function in c#, I tried to replicate it in objective-C, unfortunately it seems that I can't replicate it fully.
In short, I'm trying to add data from allDataObject array into ds array with NSNumber values from cluesToSelect array.
Below are the coding which are used. Any help to fix the issue would be much appreciated. If there are any more information that I should give, please let me know.
SelectClueViewController.m
- (IBAction)onGoPress:(id)sender {
[self chooseNumber];
NSLog(#"Array got %d numbers",dataArray.count);
}
-(void)chooseNumber
{
[dataArray removeAllObjects];
maxCount = [numberOfClues.text intValue];
int count = 0;
do {
NSInteger rdmNumber = arc4random()%objectArray.count;
if (![dataArray containsObject:[NSNumber numberWithInt:rdmNumber]])
{
NSNumber* number = [NSNumber numberWithInt:rdmNumber];
[dataArray addObject:number];
count++;
NSLog(#"random no - %d",rdmNumber);
}
} while (count < maxCount);
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:#"sendNumber"]) {
GamePageViewController *gpViewController = [segue destinationViewController];
gpViewController.cluesToSelect = self.dataArray;
NSLog(#"Success");
}
}
GamePageViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
daoDS = [[ClueDataDAO alloc]init];
self.allDataObject = daoDS.PopulateDataSource;
NSLog(#"%d",cluesToSelect.count);
[self fillDataSample];
//for keyboard
self.answer.delegate = self;
}
-(void)fillDataSample
{
int count = 0;
do {
// [self.ds addObject:[allDataObject objectAtIndex:[[cluesToSelect objectAtIndex:count]intValue] ]];
ds = [[NSMutableArray alloc]init];
currentClueData = [[ClueData alloc]init];
int firstIndex = [[cluesToSelect objectAtIndex:count]intValue];
currentClueData = [allDataObject objectAtIndex:firstIndex];
[ds addObject:currentClueData];
count++;
} while (count < cluesToSelect.count);
NSLog(#"ds got %d object",ds.count);
}
EDIT:
I am now able to make it add in object into the ds array, unfortunately it only add once. Can someone look at my fillDataSample function?
In the line you point at at the beginning :
[self.ds addObject:[allDataObject objectAtIndex:[cluesToSelect objectAtIndex:count]]];
Just a wild guess, but [cluesToSelect objectAtIndex:count] returns an object. You're trying to pass that to another objectAtIndex:, which takes an int as argument. I'm guessing the error could come from that. You could try using .intValue if it's a number.
Edit :
Here's something more readable, considering cluesToSelect and allDataObject contain only NSNumbers :
int firstIndex = [[cluesToSelect objectAtIndex:count] intValue];
int secondIndex = [[allDataObject objectAtIndex: firstIndex] intValue];
[self.ds addObject:secondIndex];
As per our discussion,
Kindly check this one:
[self.ds addObject:[allDataObject objectAtIndex:[[cluesToSelect objectAtIndex:count] intValue]]];
will do your work.
The reason as to why it didn't work for my new method of loading object from allClueData to a class object currentClueData to ds was because I did not nil and alloc init the class object after adding it to ds. The object were unable to overwrite their own value like they do in other language. (which was probably why I'm wrecking my brain for doing it in objective C) But after adding in nil the object and casting alloc and init, its working great now. thanks all :)
-(void)fillDataSample
{
int count = 0;
currentClueData = [[ClueData alloc]init];
ds = [[NSMutableArray alloc]init];
do {
// [self.ds addObject:[allDataObject objectAtIndex:[[cluesToSelect objectAtIndex:count]intValue] ]];
int firstIndex = [[cluesToSelect objectAtIndex:count]intValue];
currentClueData = [allDataObject objectAtIndex:firstIndex];
[ds addObject:currentClueData];
currentClueData =nil;
currentClueData = [[ClueData alloc]init];
count++;
} while (count < cluesToSelect.count);
NSLog(#"ds got %d object",ds.count);
}

How do you convert an argument passed to performSelector:withObject: back from type id?

I want to pass an NSInteger argument to a method called using performSelector:WithObject:afterDelay, so I have this code:
id arg = [NSNumber numberWithInt:myIdentifier];
[self performSelector:#selector(myMethod:) withObject:arg afterDelay:kDuration];
- (void) myMethod: (id) identifier
{
...
}
Within myMethod:, how do I convert the identifier from an id to an NSInteger?
I've seen this previous question:
SEL performSelector and arguments
but I don't understand myMethodForNumber: - how is that used to unbox the number?
myMethod can accept the NSNumber as the parameter. You can then get the integer value by using the NSNumber instance method.
- (void)myMethod:(NSNumber *)number {
NSInteger value = [number integerValue];
}
Update: refer to the question, please
Reading or writing with performSelector: is as following,
#import os.log;
- (void)testSEL
{
os_log(OS_LOG_DEFAULT, "Original NSObject.version is %zd", NSObject.version); // original 0
NSObject.version = 1;
os_log(OS_LOG_DEFAULT, "Custom NSObject.version is %zd (should be 1)", NSObject.version); // custom 1
// Reading
NSInteger v = (NSInteger)[NSObject performSelector:#selector(version)];
os_log(OS_LOG_DEFAULT, "Reading: version is %zd", v);
// Writing
[NSObject performSelector:#selector(setVersion:) withObject:(__bridge id)(void *)2];
os_log(OS_LOG_DEFAULT, "Writing: NSObject.version is %zd", NSObject.version);
}
You can just cast the value if you know its type:
- (void) myMethod: (id)identifier
{
NSNumber *myNumber = (NSNumber *)identifier;
}
Alternatively, you can just specify the type that the method takes:
- (void) myMethod:(NSNumber *)myNumber
{
// Use myNumber
}

Can Objective-C switch on NSString?

Is there a more intelligent way to rewrite this?
if ([cardName isEqualToString:#"Six"]) {
[self setValue:6];
} else if ([cardName isEqualToString:#"Seven"]) {
[self setValue:7];
} else if ([cardName isEqualToString:#"Eight"]) {
[self setValue:8];
} else if ([cardName isEqualToString:#"Nine"]) {
[self setValue:9];
}
Unfortunately they cannot. This is one of the best and most sought after utilizations of switch statements, so hopefully they hop on the (now) Java (and others) bandwagon!
If you are doing card names, perhaps assign each card object an integer value and switch on that. Or perhaps an enum, which is considered as a number and can therefore be switched upon.
e.g.
typedef enum{
Ace, Two, Three, Four, Five ... Jack, Queen, King
} CardType;
Done this way, Ace would be be equal to case 0, Two as case 1, etc.
You could set up a dictionary of blocks, like this:
NSString *lookup = #"Hearts"; // The value you want to switch on
typedef void (^CaseBlock)();
// Squint and this looks like a proper switch!
NSDictionary *d = #{
#"Diamonds":
^{
NSLog(#"Riches!");
},
#"Hearts":
^{
self.hearts++;
NSLog(#"Hearts!");
},
#"Clubs":
^{
NSLog(#"Late night coding > late night dancing");
},
#"Spades":
^{
NSLog(#"I'm digging it");
}
};
((CaseBlock)d[lookup])(); // invoke the correct block of code
To have a 'default' section, replace the last line with:
CaseBlock c = d[lookup];
if (c) c(); else { NSLog(#"Joker"); }
Hopefully Apple will teach 'switch' a few new tricks.
For me, a nice easy way:
NSString *theString = #"item3"; // The one we want to switch on
NSArray *items = #[#"item1", #"item2", #"item3"];
int item = [items indexOfObject:theString];
switch (item) {
case 0:
// Item 1
break;
case 1:
// Item 2
break;
case 2:
// Item 3
break;
default:
break;
}
Unfortunately, switch statements can only be used on primitive types. You do have a few options using collections, though.
Probably the best option would be to store each value as an entry in an NSDictionary.
NSDictionary *stringToNumber = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:6],#"Six",
[NSNumber numberWithInt:7],#"Seven",
[NSNumber numberWithInt:8],#"Eight",
[NSNumber numberWithInt:9],#"Nine",
nil];
NSNumber *number = [stringToNumber objectForKey:cardName];
if(number) [self setValue:[number intValue]];
A bit late but for anyone in the future I was able to get this to work for me
#define CASE(str) if ([__s__ isEqualToString:(str)])
#define SWITCH(s) for (NSString *__s__ = (s); ; )
#define DEFAULT
Here is the more intelligent way to write that. It's to use an NSNumberFormatter in the "spell-out style":
NSString *cardName = ...;
NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];
[nf setNumberStyle:NSNumberFormatterSpellOutStyle];
NSNumber *n = [nf numberFromString:[cardName lowercaseString]];
[self setValue:[n intValue]];
[nf release];
Note that the number formatter wants the string to be lowercased, so we have to do that ourselves before passing it in to the formatter.
There are other ways to do that, but switch isn't one of them.
If you only have a few strings, as in your example, the code you have is fine. If you have many cases, you could store the strings as keys in a dictionary and look up the corresponding value:
NSDictionary *cases = #{#"Six" : #6,
#"Seven" : #7,
//...
};
NSNumber *value = [cases objectForKey:cardName];
if (value != nil) {
[self setValue:[value intValue]];
}
BY FAR.. my FAVORITE "ObjC Add-On" is ObjectMatcher
objswitch(someObject)
objcase(#"one") { // Nesting works.
objswitch(#"b")
objcase(#"a") printf("one/a");
objcase(#"b") printf("one/b");
endswitch // Any code can go here, including break/continue/return.
}
objcase(#"two") printf("It's TWO."); // Can omit braces.
objcase(#"three", // Can have multiple values in one case.
nil, // nil can be a "case" value.
[self self], // "Case" values don't have to be constants.
#"tres", #"trois") { printf("It's a THREE."); }
defaultcase printf("None of the above."); // Optional default must be at end.
endswitch
AND it works with non-strings, TOO... in loops, even!
for (id ifNumericWhatIsIt in #[#99, #0, #"shnitzel"])
objswitch(ifNumericWhatIsIt)
objkind(NSNumber) printf("It's a NUMBER.... ");
objswitch([ifNumericWhatIsIt stringValue])
objcase(#"3") printf("It's THREE.\n");
objcase(#"99") printf("It's NINETY-NINE.\n");
defaultcase printf("some other Number.\n");
endswitch
defaultcase printf("It's something else entirely.\n");
endswitch
It's a NUMBER.... It's NINETY-NINE.
It's a NUMBER.... some other Number.
It's something else entirely.
Best of all, there are SO few {...}'s, :'s, and ()'s
Objective-c is no different from c in this aspect, it can only switch on what c can (and the preproc def's like NSInteger, NSUInteger, since they ultimately are just typedef'd to an integral type).
Wikipedia:
c syntax:
The switch statement causes control to be transferred to one of several statements depending on the value of an expression, which must have integral type.
Integral Types:
In computer science, an integer is a datum of integral data type, a
data type which represents some finite subset of the mathematical
integers. Integral data types may be of different sizes and may or may
not be allowed to contain negative values.
I'm kind of late to the party, but to answer the question as stated, there's a more intelligent way:
NSInteger index = [#[#"Six", #"Seven", #"Eight", #"Nine"] indexOfObject:cardName];
if (index != NSNotFound) [self setValue: index + 6];
Note that indexOfObject will look for the match using isEqual:, exactly as in the question.
Building on #Graham Perks idea posted earlier, designed a simple class to make switching on strings fairly simple and clean.
#interface Switcher : NSObject
+ (void)switchOnString:(NSString *)tString
using:(NSDictionary<NSString *, CaseBlock> *)tCases
withDefault:(CaseBlock)tDefaultBlock;
#end
#implementation Switcher
+ (void)switchOnString:(NSString *)tString
using:(NSDictionary<NSString *, CaseBlock> *)tCases
withDefault:(CaseBlock)tDefaultBlock
{
CaseBlock blockToExecute = tCases[tString];
if (blockToExecute) {
blockToExecute();
} else {
tDefaultBlock();
}
}
#end
You would use it like this:
[Switcher switchOnString:someString
using:#{
#"Spades":
^{
NSLog(#"Spades block");
},
#"Hearts":
^{
NSLog(#"Hearts block");
},
#"Clubs":
^{
NSLog(#"Clubs block");
},
#"Diamonds":
^{
NSLog(#"Diamonds block");
}
} withDefault:
^{
NSLog(#"Default block");
}
];
The correct block will execute according to the string.
Gist for this solution
You can use macros approach to achieve it:
#define CASE(str) if ([__s__ isEqualToString:(str)])
#define SWITCH(s) for (NSString *__s__ = (s); ; )
#define DEFAULT
SWITCH (string) {
CASE (#"TestString") {
break;
}
CASE (#"YetAnotherString") {
break;
}
CASE (#"Test") {
break;
}
DEFAULT {
break;
}
}
I can't Comment on cris's answer on #Cris answer but i would like to say that:
There is an LIMITATION for #cris's method:
typedef enum will not take alphanumeric values
typedef enum
{
12Ace, 23Two, 23Three, 23Four, F22ive ... Jack, Queen, King
} CardType;
So here is another One:
Link Stack over flow Go to this user answer "user1717750"
typedef enum
{
Six,
Seven,
Eight
} cardName;
- (void) switchcardName:(NSString *) param {
switch([[cases objectForKey:param] intValue]) {
case Six:
NSLog(#"Six");
break;
case Seven:
NSLog(#"Seven");
break;
case Eight:
NSLog(#"Eight");
break;
default:
NSLog(#"Default");
break;
}
}
Enjoy Coding.....

get type of NSNumber

I want to get the type of NSNumber instance.
I found out on http://www.cocoadev.com/index.pl?NSNumber this:
NSNumber *myNum = [[NSNumber alloc] initWithBool:TRUE];
if ([[myNum className] isEqualToString:#"NSCFNumber"]) {
// process NSNumber as integer
} else if ([[myNum className] isEqualToString:#"NSCFBoolean"]) {
// process NSNumber as boolean
}
Ok, but this doesn't work, the [myNum className] isn't recognized by the compiler.
I'm compiling for iPhone.
I recommend using the -[NSNumber objCType] method.
It allows you to do:
NSNumber * n = [NSNumber numberWithBool:YES];
if (strcmp([n objCType], #encode(BOOL)) == 0) {
NSLog(#"this is a bool");
} else if (strcmp([n objCType], #encode(int)) == 0) {
NSLog(#"this is an int");
}
For more information on type encodings, check out the Objective-C Runtime Reference.
You can get the type this way, no string comparisons needed:
CFNumberType numberType = CFNumberGetType((CFNumberRef)someNSNumber);
numberType will then be one of:
enum CFNumberType {
kCFNumberSInt8Type = 1,
kCFNumberSInt16Type = 2,
kCFNumberSInt32Type = 3,
kCFNumberSInt64Type = 4,
kCFNumberFloat32Type = 5,
kCFNumberFloat64Type = 6,
kCFNumberCharType = 7,
kCFNumberShortType = 8,
kCFNumberIntType = 9,
kCFNumberLongType = 10,
kCFNumberLongLongType = 11,
kCFNumberFloatType = 12,
kCFNumberDoubleType = 13,
kCFNumberCFIndexType = 14,
kCFNumberNSIntegerType = 15,
kCFNumberCGFloatType = 16,
kCFNumberMaxType = 16
};
typedef enum CFNumberType CFNumberType;
If all you want is to differentiate between booleans and anything else, you can make use of the fact that boolean NSNumbers always return a shared instance:
NSNumber *num = ...;
if (num == (void*)kCFBooleanFalse || num == (void*)kCFBooleanTrue) {
// num is boolean
} else {
// num is not boolean
}
NSNumber explicitly doesn't guarantee that the returned type will match the method used to create it, so doing this at all is probably a bad idea.
However, you could probably do something like this (you could also compare to objc_getClass("NSCFNumber") etc., but this is arguably more portable):
Class boolClass = [[NSNumber numberWithBool:YES] class];
/* ... */
if([myNum isKindOfClass:boolClass]) {
/* ... */
}
In Swift:
let numberType = CFNumberGetType(answer)
switch numberType {
case .charType:
//Bool
case .sInt8Type, .sInt16Type, .sInt32Type, .sInt64Type, .shortType, .intType, .longType, .longLongType, .cfIndexType, .nsIntegerType:
//Int
case .float32Type, .float64Type, .floatType, .doubleType, .cgFloatType:
//Double
}
Use the method -[NSNumber objCType] method to get the type.
If the type's equal to #encode(BOOL), or the number itself is kCFBooleanFalse, or kCFBooleanTrue, it's a boolean.
If it's anything else but 'c', it's a number.
If it's 'c', what appears to be the only way supported way, without checking against private class names, or comparing against undocumented singletons, is to turn make an array of one element, the number, and then use NSJSONSerialization to get the string representation. Finally, check if the string representation contains the string "true" or "false". Here is the full code for checking if an NSNumber is a BOOL:
-(BOOL)isBool
{
if(!strcmp(self.objCType, #encode(BOOL)) ||
self == (void*)kCFBooleanFalse ||
self == (void*)kCFBooleanTrue)
{
return YES;
}
if(strcmp(self.objCType, "c"))
{
return NO;
}
NSString * asString = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:#[self] options:kNilOptions error:nil] encoding:NSUTF8StringEncoding];
return [asString containsString:#"true"] || [asString containsString:#"false"];
}
Note that using NSJSONSerialization is slow and if #NO/#YES ever stops always equalling kCFBooleanFalse/kCFBooleanTrue, then this method probably shouldn't be used in a tight loop.
The reason the compiler warns you and it doesn't work is because -[NSObject className] is declared in a category on NSObject on Mac OS X (in NSScriptClassDescription.h) and not declared on iPhone. (It doesn't support AppleScript, obviously.) NSStringFromClass([myNum class]) is what you should use to be safe across all platforms. Odds are that -className is declared as a simple wrapper around NSStringFromClass() anyway...
NSString *classString = NSStringFromClass([myNum class]);
That should ger the string you want.
To check that NSNumber contains a bool value Try this:
if (strcmp([myNumber objCType], [#(YES) objCType]) == 0)
NSLog(#"%#", [myNumber boolValue] ? #"true" : #"false");
objCType documentation states that The returned type does not necessarily match the method the number object was created with
Secondly, other methods of comparing the class of number to a given class type or assuming boolean number instances to be shared singletons are not documented behaviour.
A more(not completely though) reliable way is to depend on NSJSONSerialisation as it correctly recognises number instances created with bool and outputs true/false in json. This is something we can expect Apple to take care of while moving with new SDKs and on different architectures. Below is the code:
+(BOOL) isBoolType:(NSNumber*) number {
NSError* err;
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:#{#"key":number}
options:0
error:&err];
NSString* jsonString = [[NSString alloc]
initWithData:jsonData
encoding:NSUTF8StringEncoding];
return [jsonString containsString:#"true"]
|| [jsonString containsString:#"false"];
}
Swift Version
NSNumber is a class-cluster so each underlying type can be figured from the instance. This code avoids hard-coding the different NSNumber types by creating an instance of the expected type, and then comparing it against the unknown type.
extension NSNumber {
var isBool: Bool {
return type(of: self) == type(of: NSNumber(booleanLiteral: true))
}
}
check object is of NSNumber type :
if([obj isKindOfClass:NSClassFromString(#"__NSCFNumber")])
{
//NSNumber
}