Use return value as argument to method - iphone

Is it possible to use the return value of a method as the argument for a different method invocation? I'm using Objective-C.
What I'm going for is something like this:
stringOutput = [object1 method1:[object2 method2:[object3 method3]]];
where method 3 returns a string that goes into method 2, which returns a string that goes into method 1, which returns a string that goes into stringOutput.

Do you mean sending the result from one method as the parameter for another?
NSString *string = [self myMethod:[self myMethod2]];
Where the methods are
- (NSString *)myMethod2 {
return #"A String";
}
- (NSString *)myMethod:(NSString *)string {
// Do something with string
// Return another string
return #"Something else";
}

Related

Shorten url using bit.ly

I have tried to Shorten urls using bit.ly. When i try to pass a static link it gives me a shorten url but when i try to pass a variable link it doesn't.
here is my code....
Bitlyzer *bitlyzer = [[Bitlyzer alloc] initWithDelegate:self];
[bitlyzer shortURL:string];
[bitlyzer shortURL:#"http://www.google.com"];
When i pass this url it gives me a Shorten url but when i pass a variable string as shown above it doesn't give me shorten url.
Please give me your suggetions...
Some time in our string some space is remain and so bitly not convert it and return null value so first remove the null or space from string and then try to convert it..
Add my these two methods in your .m file and then use with your variable.. see the example also how to use it...
-(NSString*) trimString:(NSString *)theString {
NSString *theStringTrimmed = [theString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
return theStringTrimmed;
}
-(NSString *) removeNull:(NSString *) string {
NSRange range = [string rangeOfString:#"null"];
//NSLog(#"in removeNull : %d >>>> %#",range.length, string);
if (range.length > 0 || string == nil) {
string = #"";
}
string = [self trimString:string];
return string;
}
And use this like bellow...
string = [self removeNull:string];
[string retain];
Bitlyzer *bitlyzer = [[Bitlyzer alloc] initWithDelegate:self];
[bitlyzer shortURL:string];

Method to put NSString and NSNumber into array

It's a calculator. I have a display where I can put digits and variables (x, y etc.). When I push Enter button it sends what is on display to array with all operand.
As on display can be NSString (variables) or NSNumber (digits) I thought to use "id" as method argument.
- (IBAction)enterPressed
{
[self.brain pushOperand:self.display.text];
}
/////////////////////
- (void) pushOperand:(id)operand
{
////// So if operand is digit I need to transform it into NSNumber.
NSNumber *digitToStack = [NSNumber numberWithDouble:operand];
/////// Here is problem - "Sending '___strong id' to parameter of incompatible type 'double'
NSNumber *digitToStack = [operand doubleValue];
//////// If i do like this, i have warning - "Initializing 'NSNumber *__strong' with an expression of incompatible type 'double'
[self.programStack addObject:operand];
}
I don't understand what this warnings are all about.
So the question is can I somehow put in Array NSNumber and NSString using id method, or how should I do it?
Can i 'transform' argument from 'id' method into NSNumber?
Yes you can "transform" your argument of operand, but you'd need to do a cast.
Also, the line:
NSNumber *digitToStack = [NSNumber numberWithDouble:operand];
fails because "operand" is an Objective C object while that function is expecting a C-style double type (which is NOT an Objective C object).
Here's some code I wrote off the top of my head:
// Let's make operand always be a NSString object
// since that's what is being passed in from the label
- (void) pushOperand:(NSString *)operand
{
double doubleValueFromOperand = [operand doubleValue];
if(fabs(doubleValueFromOperand) != HUGE_VAL)
{
NSNumber *digitToStack = [NSNumber numberWithDouble:doubleValueFromOperand];
if(doubleValueFromOperand != 0.0)
{
[self.programStack addObject:digitToStack];
return;
} else {
// because NSString's doubleValue also returns 0.0 for a
// non-numerical string, let's make sure the input from the label
// is not 0.0
if([operand compare: #"0.0" options: NSCaseInsensitiveSearch range: NSMakeRange(0, 3)] == NSOrderedSame)
{
// the operand string *is* a 0.0 input, so let's add it to your stack and return
[self.programStack addObject: digitToStack];
return;
}
}
}
// if we get to this point, we probably have a non-numerical string object
[self.programStack addObject: operand];
}
This code hasn't been tested, has no warranties, and could certainly use a further cleaning up and optimization (e.g. the check for "0.0" isn't what I would put into production code, myself).
But hopefully this is enough to get you further along, Sasha!

Pass an NSString variable to a function and modify it

I'm new in objective-c, this is my first post. This is my problem:
In my main file I have a variable: NSString *var;
Then I have a function:
-(BOOL) myfunction:(NSString *)myvar {
myvar = #"OK!";
return YES;
}
Now in the main file I do:
NSString *var;
BOOL control = myfunction:var;
if(control)
NSLog(#"var is: %#",var);
but the output is "var is: ". If I built in debug mode, and put a breakpoint at the start of function myfunction the memory address of myvar is equal to var, but after the assignment the address of myvar is different from the address of var. How can I solve it? thanks in advance!
While the answers given so far are correct. I think a more pertinent question is why you would want to do this.
It looks you want to have a defensive programming style where the return code indicates success or failure.
A more Objective-C like way to do this would be to pass the string and an NSError ** as a parameter and return the string or a nil to indicate failure as described in the Error Handling Programming Guide
So the way to write this would be:
- (NSString *)aStringWithError:(NSError **)outError {
returnString = #"OK";
if (!returnString) {
if (outError != NULL) {
NSString *myErrorDomain = #"com.company.app.errors";
NSInteger errNo = 1;
*outError = [[[NSError alloc] initWithDomain:myErrorDomain code:errNo userInfo:nil] autorelease];
}
}
return returnString;
}
And to use it:
NSError *error;
NSString *stringVariable = [self aStringWithError:&error];
if (!stringVariable) {
// The function failed, so it returned nil and the error details are in the error variable
NSLog(#"Call failed with error: %ld", [error code]);
}
This is a trivial example, but you can see that you can construct and return far more meaningful information about the error rather than just whether it succeeded or not.
You can also use NSMutableString.
-(BOOL) myfunction:(NSMutableString *)myvar {
[myvar setString:#"OK!"];
return YES;
}
and change your call site as follows:
NSMutableString *var = [NSMutableString string];
BOOL control = [self myfunction:var];
if(control)
NSLog(#"var is: %#",var);
Syntactically you need to fix your myFunction invocation like so:
BOOL control = [self myfunction:var];
The real problem here is that inside of myFunction you are assigning a string value to a local string variable only, whereas you want to change the underlying string that it points to. This is usually the wrong approach in Obj-C, but you can do it like so:
-(BOOL)myfunction:(NSString **)myvar
{
*myvar = #"OK!";
return YES;
}
NSString *var;
BOOL control = [self myfunction:&var];
if(control)
NSLog(#"var is: %#",var);

Assigning the value of a method to a variable

In Objective-C, how do I assign the return value of a method to a variable of the same type?
Are you having any problem with:
- (NSString *)getString {
return #"hehe";
}
NSString *myString = [self getString];
You need to put the method getString above the assigning or defining the getString in the interface
Say your method prototype looked like this:
- (NSString *) name;
You'd create a variable to hold the return value like this:
NSString *some_name = [obj name];
Suppose you method returns an array
-(NSMutableArray *)getArray;
then NSMutableArray *arr=[self getArray];
or if a function return an object of a class
-(Class *)getObjectOfClass;
then you can hold this object like
Class *objClass=[self getObject];
means by simply assigning the return value to same type of variable.you can access that.

how to pass variable arguments to another method?

i have googled and came to know that how to use the variable arguments. but i want to pass my variable arguments to another method. i m getting errors. how to do that ?
-(void) aMethod:(NSString *) a, ... {
[self anotherMethod:a];
// i m doing this but getting error. how to pass complete vararg to anotherMethod
}
AFAIK ObjectiveC (just like C and C++) do not provide you with a syntax that allows what you directly have in mind.
The usual workaround is to create two versions of a function. One that may be called directly using ... and another one called by others functions passing the parameters in form of a va_list.
..
[obj aMethod:#"test this %d parameter", 1337);
[obj anotherMethod:#"test that %d parameter", 666);
..
-(void) aMethod:(NSString *)a, ...
{
va_list ap;
va_start(ap, a);
[self anotherMethod:a withParameters:ap];
va_end(ap);
}
-(void) anotherMethod:(NSString *)a, ...
{
va_list ap;
va_start(ap, a);
[self anotherMethod:a withParameters:ap];
va_end(ap);
}
-(void) anotherMethod:(NSString *)a withParameters:(va_list)valist
{
NSLog([[[NSString alloc] initWithFormat:a arguments:valist] autorelease]);
}
You cannot pass variadic arguments directly. But some of these methods provide an alternative that you can pass a va_list argument e.g.
#include <stdarg.h>
-(void)printFormat:(NSString*)format, ... {
// Won't work:
// NSString* str = [NSString stringWithFormat:format];
va_list vl;
va_start(vl, format);
NSString* str = [[[NSString alloc] initWithFormat:format arguments:vl] autorelease];
va_end(vl);
printf("%s", [str UTF8String]);
}
Have you considered setting up your arguments in either an array or dictionary, and coding conditionally?
-(void) aMethodWithArguments:(NSArray *)arguments {
for (id *object in arguments) {
if ([object isKindOfClass:fooClass]) {
//handler for objects that are foo
[self anotherMethod:object];
}
if ([object isKindOfClass:barClass]) {
//and so on...
[self yetAnotherMethod:object];
}
}
}
I think you could use macros to achieve same thing.
Let's say you wanna pass aMethod's variable arguments to another
-(void) aMethod:(NSString *) a, ... {
}
You could define your another 'method' using macro though it is not a real method:
#define anotherMethod(_a_,...) [self aMethod:_a_,##__VA_ARGS__]
This is my solution.