Variadic function without nil termination - cocoa-touch

I am trying to create a method like the following:
- (void)setCondition:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);
But since I'm not great with preprocessor, I hit an issue that I have fixed in the following code snippet, but I'd like to know if there's not cleaner way to achieve what I want which is to stop after the provided arguments
+ (CRCondition *)conditionWithFormat:(NSString *)format,... {
CRCondition *condition = [[CRCondition alloc] init];
NSArray *conditionSliced = [condition sliceFormatOperationFromString:format];
condition->_leftOperand = [[conditionSliced objectAtIndex:0] retain];
condition->_operator = [condition operatorFromString:[conditionSliced objectAtIndex:1]];
condition->_rightOperand = [[conditionSliced objectAtIndex:2] retain];
id eachObject;
va_list argumentList;
va_start(argumentList, format);
while ((eachObject = va_arg(argumentList, id))) {
if ([condition->_leftOperand isEqualToString:#"%K"]) {
[condition->_leftOperand release];
if ([eachObject isKindOfClass:[NSString class]])
condition->_leftOperand = [eachObject retain];
else
condition->_leftOperand = [[eachObject description] retain];
}
else if ([condition->_rightOperand isKindOfClass:[NSString class]] &&
[condition->_rightOperand isEqualToString:#"%#"]) {
[condition->_rightOperand release];
condition->_rightOperand = [eachObject retain];
}
else
break;
}
va_end(argumentList);
if (![condition isOperatorValid]) {
NSException *exception = [NSException exceptionWithName:#"Invalid Condition Operator"
reason:#"The operator passed is invalid. Must follow the following regex pattern: ([(=><)|(A-Z)]{1,2})"
userInfo:nil];
[exception raise];
}
return [condition autorelease];
}
The problem is with the while loop that circles and go past the provided arguments (I'm aware of why it's providing me other value, cmd args and such)
If you need any more explanation please add comments so I can get back to you.

The usual approach would be to parse the format string first and figure out how many arguments should follow it based on that (there is usually exactly one valid number of arguments for any format string). If the number of arguments is not deducible from a format string, it is common to terminate the list with nil (as in arrayWithObjects:...).

Related

How to create a method that takes undefined number of arguments in Objective-C [duplicate]

Maybe this will be obviously simple for most of you, but could you please give an example how to create similar methods (in Objective-C) and functions in C to create functions like NSString's stringWithFormat:, or NSLog().
Just to remind:
[NSString stringWithFormat:#"example tekst %i %# %.2f", 122, #"sth", 3.1415"];
NSLog(#"account ID %i email %#", accountID, email);
I'd like to create the similar to NSString's method stringWithFormat:, NSURL - urlWithFormat.
What these are called, generally, is "variadic functions" (or methods, as it were).
To create this, simply end your method declartion with , ..., as in
- (void)logMessage:(NSString *)message, ...;
At this point you probably want to wrap it in a printf-like function, as implementing one of those from scratch is trying, at best.
- (void)logMessage:(NSString *)format, ... {
va_list args;
va_start(args, format);
NSLogv(format, args);
va_end(args);
}
Note the use of NSLogv and not NSLog; consider NSLog(NSString *, ...); vs NSLogv(NSString *, va_list);, or if you want a string; initWithFormat:arguments: on NSString *.
If, on the other hand, you are not working with strings, but rather something like
+ (NSArray *)arrayWithObjects:(id)object, ... NS_REQUIRES_NIL_TERMINATION;
things get a lot easier.
In that case, instead of a vprintf-style function, use a loop going through args, assuming id as you go, and parse them as you would in any loop.
- (void)logMessage:(NSString *)format, ... {
va_list args;
va_start(args, format);
id arg = nil;
while ((arg = va_arg(args,id))) {
/// Do your thing with arg here
}
va_end(args);
}
This last sample, of course, assumes that the va_args list is nil-terminated.
Note: In order to make this work you might have to include <stdarg.h>; but if memory serves, this gets included in connection with NSLogv, meaning it comes down by way of "Foundation.h", therefore also "AppKit.h" and "Cocoa.h", as well as a number of others; so this should work out of the box.
- (void)methodWithFormat:(NSString*)format, ... {
va_list args;
va_start(args,format);
//loop, get every next arg by calling va_arg(args,<type>)
// e.g. NSString *arg=va_arg(args,NSString*) or int arg=(args,int)
va_end(args);
}
If you want to pass the variable arguments to stringWithFormat:, use something like:
NSString *s=[[[NSString alloc] initWithFormat:format arguments:args] autorelease];
One thing to mention here is that, the first NSString parameter here comes as format, and the other are passed in the variable argument. right? So before entering the for loop, you have one parameter to handle.
- (NSString *) append:(NSString *)list, ...
{
NSMutableString * res = [NSMutableString string];
[res appendString:list];
va_list args;
va_start(args, list);
id arg = nil;
while(( arg = va_arg(args, id))){
[res appendString:arg];
}
va_end(args);
return res;
}
- (void) test_va_arg
{
NSString * t = [self append:#"a", #"b", #"c", nil];
STAssertEqualObjects(#"abc", t, #"");
}

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.

Method returning 0

I'm doing the foundation calculator homework from the cs193p course, and my
+evaluateExpression:usingVariables: method doesn't work. It always returns 0.
Here's my method:
+ (double)evaluateExpression: (id)anExpression usingVariablevalues: (NSDictionary *)variables {
CalculatorBrain *worker = [[[CalculatorBrain alloc] init] autorelease];
double returnValue = 10.0;
if ([anExpression isKindOfClass:[NSMutableArray class]]) {
for (id term in anExpression) {
NSLog(#"%f", returnValue); // breakpoint
if ([term isKindOfClass:[NSString class]]) { // string
if ([term hasPrefix:#"%"] && [term length] == 2) { // variable
double value = [(NSNumber *)[variables objectForKey:[term substringFromIndex:1]] doubleValue];
NSLog(#"Variable: %#, value: %d", [term substringFromIndex:1], value);
[worker setOperand:value];
}
else if ([term length] == 1) { // operation
returnValue = [worker performOperation:term];
}
else {
NSLog(#"Invalid expression.\n\tMultiple character operation found: %#", term);
}
}
else if ([term isKindOfClass:[NSNumber class]]) { // operand
double value = [term doubleValue];
[worker setOperand:value];
}
else {
NSLog(#"Invalid expression.\n\tWrong type in expression: %#, The value is: %#.",[term class], term); // Wrong type in expression
}
}
}
else {
NSLog(#"Invalid expression.\n\tThe expression is of the wrong type: %#.", [anExpression class]); // expression is of wrong type
}
//returnValue = worker.operand;
return returnValue;
}
Any hints? it always returns 0.
/* Ouput from Console */
2012-06-27 20:01:05.487 Calculator[2823:207] 0
2012-06-27 20:01:05.488 Calculator[2823:207] 0
2012-06-27 20:01:05.489 Calculator[2823:207] 0
2012-06-27 20:01:05.490 Calculator[2823:207] Variable: x, value: 0
2012-06-27 20:01:05.490 Calculator[2823:207] 0
2012-06-27 20:01:05.491 Calculator[2823:207] 0
2012-06-27 20:01:05.491 Calculator[2823:207] 0
Note: I set returnValue to 10.0 at the beginning to check if it is getting changed at all.
Update:
I found out that it gets 0 from the dictionary. I think the calling code is the cause:
- (IBAction) performSampleExpression {
NSMutableArray *expr = [[NSMutableArray alloc] init];
[expr addObject:[NSNumber numberWithDouble:3.0]];
[expr addObject:#"+"];
[expr addObject:#"%x"];
[expr addObject:#"*"];
[expr addObject:[NSNumber numberWithDouble:4.0]];
NSDictionary *varsdict = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithDouble:10.0], #"x", nil];
double result = [CalculatorBrain evaluateExpression:expr usingVariablevalues:varsdict];
if (!result) NSLog(#"result = nil");
NSLog(#"%f", result);
display.text = [NSString stringWithFormat:#"%f", result];
[expr release];
}
Result isn't nil though.
Update 2:
Using the debugger (nice hint. This might solve it.) I found something strange:
On this line: (first time)
returnValue = worker.operand;
It says returnValue is still 10, and not three (worker.operand IS).
Is the assignment failing, or is this how it should be? (Just wondering).
UPDATE 3:
Okay there is something very strange going on here: I set a breakpoint on the return statement, the last line of +evaluateExpression:usingVariables:, and it says
returnValue = 4.
This means, the actual problem lies in -performSampleExpression. What am I doing wrong?
UPDATE 4:
Changing #"%d", double into #"%f", double helped a lot. That explains the strange Console output. But, it solved it, because I updated the display like display [setText:#"%d", result]; what caused my double to be displayed as 0 due to a wrong cast.
I discovered this by using the debugger, so the one who suggested that practically solved it. (And I asked for hints, not for solutions. After all, homework is to learn from).
The possible issues can shown in:
returnValue = [worker performOperation:term];
and
returnValue = worker.operand;
You should stop debugger in this line (or line below) and watch how to your value of returnValue change.
You stop app clicking on position where it is on the screen, app stops, and then jump to next line with F6 (or Fn+F6). Below (in console) you can see the value.
EDIT
You could try this solution - Change allocation to:
CalculatorBrain *worker = [[[CalculatorBrain alloc] init] autorelease];
And delete line:
[worker release];
This release right before return may couse the problem without NSCopying #protocol.
Autorelease and NSCopying
For better understanding compile this code:
NSMutableArray *arrOne = [[NSMutableArray alloc] initWithObjects:#"1",#"2", nil];
NSMutableArray *arrTwo = [[NSMutableArray alloc] init];
arrTwo = arrOne;
[arrOne addObject:#"3"]; //After that arrTwo "shold" be 1,2 and arrOne 1,2,3. No! There both 1,2,3!
for(int i=0;i<[arrTwo count];i++)
NSLog(#"%#",[arrTwo objectAtIndex:i]);
So you may consider how you assign a variable. In above example releasing memory shouldn't effect return values, but by using autorelease with return object you don't have to assign anything.
Have you tried stepping through the code a line at a time and examining the variable values?
I'm seeing a little memory leak in your code:
so, instead of this:
[worker release];
try this line: [worker autorelease];
I'm not sure it caused the problem but your version definitely does not look good.

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.....

Objective c implement method which takes array of arguments

Hee
Does anybody know how to implement an method in objective c that will take an array of arguments as parameter such as:
[NSArray arrayWithObjects:#"A",#"B",nil];
The method declaration for this method is:
+ (id)arrayWithObjects:(id)firstObj...
I can't seem to make such method on my own. I did the following:
+ (void) doSometing:(id)string manyTimes:(NSInteger)numberOfTimes;
[SomeClass doSometing:#"A",#"B",nil manyTimes:2];
It will give the warningtoo many arguments to function 'doSometing:manyTimes:'
Thanks already.
The ellipsis (...) is inherited from C; you can use it only as the final argument in a call (and you've missed out the relevant comma in your example). So in your case you'd probably want:
+ (void)doSomethingToObjects:(id)firstObject, ...;
or, if you want the count to be explicit and can think of a way of phrasing it well:
+ (void)doManyTimes:(NSInteger)numberOfTimes somethingToObjects:(id)firstObject, ...;
You can then use the normal C methods for dealing with ellipses, which reside in stdarg.h. There's a quick documentation of those here, example usage would be:
+ (void)doSomethingToObjects:(id)firstObject, ...
{
id object;
va_list argumentList;
va_start(argumentList, firstObject);
object = firstObject;
while(1)
{
if(!object) break; // we're using 'nil' as a list terminator
[self doSomethingToObject:object];
object = va_arg(argumentList, id);
}
va_end(argumentList);
}
EDIT: additions, in response to comments. You can't pass the various things handed to you in an ellipsis to another function that takes an ellipsis due to the way that C handles function calling (which is inherited by Objective-C, albeit not obviously so). Instead you tend to pass the va_list. E.g.
+ (NSString *)doThis:(SEL)selector makeStringOfThat:(NSString *)format, ...
{
// do this
[self performSelector:selector];
// make string of that...
// get the argument list
va_list argumentList;
va_start(argumentList, format);
// pass it verbatim to a suitable method provided by NSString
NSString *string = [[NSString alloc] initWithFormat:format arguments:argumentList];
// clean up
va_end(argumentList);
// and return, as per the synthetic example
return [string autorelease];
}
Multiple arguments (also known as an arglist) can only come at the end of a method declaration. Your doSomething method would look something like this:
+ (void)doNumberOfTimes:(NSInteger)numberOfTimes withStrings:(id)firstArg, ...
{
va_list args;
va_start(args, firstArg);
NSString * argString = firstArg;
while (argString != nil)
{
// do something with argString here
argString = va_arg(args, NSString *);
}
va_end(args);
}
To be called as follows:
[SomeClass doNumberOfTimes:2 withStrings:#"A", #"B", nil];
See also: How to create variable argument methods in Objective-C
I think you're after a variadic function. Here's Apple's documentation: http://developer.apple.com/library/mac/qa/qa2005/qa1405.html