How to pass the parameter
-(void)errorValue:(void(^)(NSError*))error{
[self errMssg];
}
-(void)call{
(void(^)(NSError*))error;
[self errorValue ?];
}
Please let me know how to pass (void(^)(NSError*))error to the method!
#All
Thanks in Advance
You need to properly declare a block variable first. Then you just pass it by name like any other variable:
void(^myBlock)(NSError *) = ^(NSError* error) {
// Do something
};
[self errorValue:myBlock];
Alternatively, you can pass a block literal directly:
[self errorValue:^(NSError* error) {
// Do something
}];
Related
I have the following code:
- (NSString*)returnIncorrectDef {
NSInteger defIndex = [self randomIndex];
NSString *incorrectDef = [NSString stringWithFormat:#"%#", [definitions objectAtIndex:defIndex]];
if (([incorrectDef isEqualToString:self.definitionString]) || ([incorrectDef isEqualToString:def1.titleLabel.text])) {
// I want to restart the method here
[self returnIncorrectDef];
} else {
return incorrectDef;
}}
I want to be able to restart this method until the if clause is not met. However when I try this code I get an error saying: "Control may reach end of non-void function". How would I do this?
Use a while loop with a flag:
- (NSString*)returnIncorrectDef {
BOOL done = NO;
NSString *incorrectDef = nil;
while (!done) {
NSInteger defIndex = [self randomIndex];
incorrectDef = [definitions objectAtIndex:defIndex];
done = !([incorrectDef isEqualToString:self.definitionString] ||
[incorrectDef isEqualToString:def1.titleLabel.text]);
}
return incorrectDef;
}
You can also use do ... while statement.
Note that [NSString stringWithFormat:#"%#",...] is a pointless statement and has been simplified. (see #medvedNick's comment - it's not pointless if that class isn't an NSString class).
IMPORTANT: Without the while loop, you are using recursion, which could potentially crash your program if you keep "missing" your target (which is certainly possible).
trojanfoe's solution is better, but the issue you have is that you never return the value that you get when you reenter. CHanging that line to
return [self returnIncorrectDef];
should fix the problem
Instead of [self returnIncorrectDef]; simply do:
return [self returnIncorrectDef];
That way you return a value by recursively calling your function. Be sure that the evaluation parameters change though, to prevent an infinite loop
EDIT but this might not be what you want in this particular case (calling the method over and over again)
I have 4 methods
In Class A
-(void)Method1
{
}
In Class B
-(void)Method2
{
//calling method3 here
}
-(void)Method3
{
//calling method4 here
}
-(NsMutablearray*)Method4
{
return array;
}
I am calling method2 from method1, and it goes upto method4 and method4 returns an nsmutablearray. How can i get that array in my method1.
Change your methods like:
-(void)Method1
{
NSMutableArray *tempArray = [classbObj method2];
}
In Class B
-(NSMutablearray *)Method2
{
return [self method3];
}
-(NSMutablearray *)Method3
{
return [self method4];
}
-(NSMutablearray*)Method4
{
return array;
}
You have few ways to accomplish this task :
First :Need to change your method signature, and use as :
-(void)method1{
ClassB *classBObj=[ClassB new];
NSMutableArray *tempArray = [classBObj method2];
}
In ClassB
-(NSMutableArray *)method2{
return [self method3];
}
-(NSMutableArray *)method3{
return [self method4];
}
-(NSMutableArray *)method4{
...
return array;
}
Second : If you want same array as in method4 in method1 without any modifications, post a notification, and observe it in ClassA.
Third: You can use delegate for this, as this is 1-to-1, this will be better than notification.
Fourth: If you dont want to change your method signature create a shared class having a property of type NSArray and use that property to pass your arrays within the methods and classes.
You have call method 2 like this. In method4Ary you will have returned value
NSMutableArray * method4Ary = [method1_obj Method2]
Use delegate for backward messaging in class.
Refer Basic Delegate Example link for more reference.
Just change the return type from void to NsMutablearray
I want to call another method from the updateButtonPressed method.
This is what I tried:
-(IBAction) updateButtonPressed{
[self loadScrollViewWithPage];
}
But the problem is that the loadScrollViewWithPage method has arguments. That method is like this:
- (void)loadScrollViewWithPage:(int)page {
}
How can I call this method?
If I understand correctly, you are wondering how to pass arguments along with messages to objects, is that right? Try:
-(IBAction) updateButtonPressed{
int foo = 4;
[self loadScrollViewWithPage:foo]; // a colon, followed by the argument
}
I suggest you read up on the Objective-C language in general, though.
http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/objectivec/introduction/introobjectivec.html
- (IBAction) updateButtonPressed{
int tempValue=5;
[self loadScrollViewWithPage:tempValue];
}
-(void) callme {
//statements
here I call another method "callmeagain"
}
}
But it is not working. Is there another way to do it?
To call an ObjC method, use the syntax [foo methodName:param andAlso:param2 …] In your case, try
-(void)callme {
[self callmeagain];
}
Another Method could be
[self performSelector:#selector(callmeagain)];
It is basically the same thing as Kenny's Suggestion
I get a null return when i try out my NSString function.
//Track.m
static NSString* trackUrl;
//static NSString* getTrackNumberUrl;
#implementation Track
- (NSString*)trackUrl {
return #"http://site.com/?a=";
}
- (NSString*)setTrackNumberUrl:(NSString*)trackNumberUrl {
if (trackUrl != trackNumberUrl) {
return [trackUrl stringByAppendingFormat:trackNumberUrl];
}
return #"Error no trackNumber";
}
- (NSString*)getTrackNumberUrl:(NSString*)trackNumber {
return [[[self alloc] setTrackNumberUrl:trackNumber] autorelease];
}
#end
MainView.m, just to show the return answer in NSlog
- (NSString *) trackNumber{
return [track getTrackNumberUrl:#"86147224549XX"];
}
- (void)drawRect:(CGRect)rect {
NSLog(trackNumber);
}
I get a null return answer? Have i miss something? Thanks.
Edit some in Track.m
- (NSString*)setTrackNumberUrl:(NSString*)trackNumberUrl {
if (trackUrl != trackNumberUrl) {
return [trackUrl stringByAppendingString:trackNumberUrl];
}
return #"Error no trackNumber";
}
- (NSString*)getTrackNumberUrl:(NSString*)trackNumber {
return [[[Track alloc] setTrackNumberUrl:trackNumber] init];
}
This is how it should work.
getTrackNumberUrl --> setTrackNumberUrl --> trackUrl (return) --> setTrackNumberUrl + trackNumber --> getTrackNumberUrl (trackNumberUrl = trackUrl + trackNumber)
I have this code to set reference to Track
#class Track;
#interface MainView : UIView {
Track *track;
}
#property (nonatomic, retain) IBOutlet Track *track;
Well if don't should use self alloc, what should i use?
You have a lot of problems with your code.
return [trackUrl stringByAppendingFormat:trackNumberUrl];
You should not use an arbitrary string as a format, because if it contains a format specifier like "%d" then the method will go looking for a variable that isn't there, and will likely crash. You should use stringByAppendingString: instead. However, that doesn't seem to be what you want here, since the method name is setTrackNumberUrl:. If you want to change the value of the trackUrl variable, you can't call stringByAppendingFormat:; all that does is return a new string and leave the original alone. I think you simply want something like
[trackUrl release];
trackUrl = [trackNumberUrl retain];
Another problem:
return [[[self alloc] setTrackNumberUrl:trackNumber] autorelease];
In this context, self is an instance of Track. An instance won't understand the alloc message, that must be sent to a class. It will return a new instance, to which you should send an init message. So you would do something like [[Track alloc] init].
NSLog(trackNumber);
The first parameter to NSLog is a format string, so for the same reasons as above you shouldn't use a variable, you should do something like this: NSLog(#"%#", trackNumber); That line of code prints the value of the variable, trackNumber. Considering that you have a method named trackNumber just above it, I wonder if what you really want to do is call the method and get the result. In that case, you need to write it as [self trackNumber] which will call the method and return an NSString.
Most probably track is nil in the trackNumber - have you set it to a correct reference to a Track object?
Also, this code
- (NSString*)getTrackNumberUrl:(NSString*)trackNumber {
return [[[self alloc] setTrackNumberUrl:trackNumber] autorelease];
}
is incorrect. Why are you using [self alloc]? You're allocating a new Track object (using a static method on an object reference, not on a class name, which is an error), setting it's track number URL, and returning an autoreleased NSString, but you're leaking the Track object you allocated.
return [trackUrl stringByAppendingFormat:trackNumberUrl];
I'm not sure bout this one,
try using it as a format for string.
return [trackUrl stringByAppendingFormat:#"%#",trackNumberUrl];