Objective-C: blocks and ARC [closed] - ios

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have created utility class for UIAlerts. I am using blocks
My code, that user should create, looks like this:
MyAlertMessage * a = [[MyAlertMessage alloc] initWithTitle:#"Hello" WithMessage:#"World"];
[a addButton:BUTTON_OK WithTitle:#"OK" WithAction:^(void *action) {
NSLog(#"Button OK at index 0 click");
}];
[a addButton:BUTTON_CANCEL WithTitle:#"Cancel" WithAction:^(void *action) {
NSLog(#"Button Cancel at index 1 click");
}];
[a show]
Full class can be seen here: https://github.com/MartinPerry/UIAlert
Now, If I do this, after [a show] ARC destroys my class, so blocks are no longer working and gives me error. I have solved this by creating singleton class that holds reference to created MyAlertMessage (message ads and destroys itself from this manager). Is this the correct solution or it should be done better, without this singleton manager?
Manager and appropriate class can be found here: https://github.com/MartinPerry/UIAlert/blob/master/UIAlert/MyAlertMessage.m

The example code you posted uses a local variable to create a MyAlertMessage object. Since it is a local variable, it goes out of scope as soon as execution leaves the current set of braces (method, if statement, whatever.) As soon as that happens nobody has a strong reference to the object, so it gets deallocated.
Make the MyAlertMessage variable an instance variable and set it to nil when you are done with it.

Related

How to use singleton object into a objective-C Block [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Using a singleton object in a block will create a strong reference cycle in the code ?
As I have more then 5 singleton object in application.
You have to use weak reference of singleton inside blocks.
YourSingleton *singletonInstance = ---- //get your singleton instance here
typeof(YourSingleton) __weak weakSingletonInstance = singletonInstance;
// your block
^
{
// Now use weakSingletonInstance inside the block.
}];

How to write a completion block in objective-C? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm having trouble creating completion blocks. I found the solution here
. It works for me, but I don't quite understand this. Now I'm still confused and don't know how to write a block myself. Are there any batter ways to understand blocks? Also, when should I use it? Is there anything that can replace blocks?
Should I create it as a property? Method perimeter? Do they have difference in efficiency?
Thank you!
check out http://fuckingblocksyntax.com for syntax.
For personal choice I like to return value and error in the completion block (similar to iOS framework pattern)
As an example;
declaration
- (void)fetchStuff:(void (^)(id value,NSError *error))completion;
calling the function
// async fetch
[object fetchStuff:^(id value, NSError *error) {
// do stuff with value
}];

Which one of these would cause a retain cycle? (obj-c, sample code) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Still trying to get the hang of retain cycles when using blocks.
My question is.. which of the following (if any) would cause retain cycles?
1
[self.someProperty runSomeBlock:^{
[self.someOtherProperty doSomething];
}];
2
[self.someProperty runSomeBlock:^{
[self doSomething];
}];
3
[self.someProperty runSomeBlock:^{
[someObject runAnotherBlock:^{
[self.someProperty doSomething];
}];
}];
4
[self.someProperty runSomeBlock:^{
[someObject runAnotherBlock:^{
[self.someOtherProperty doSomething];
}];
}];
Thanks!
None of them, on the face of it. The thing that causes a retain cycle with a block is e.g. when the thing you hand the block to persists and retains it (over time) and you retain that thing over time, and the block mentions you — and there's no obvious evidence that that would be happening here.
In other words, it's really no different from the basic thing that always causes a retain cycle: A retains B but B retains A. But in your code, I see no evidence that anyone is retaining anyone.
In any case, if all the objects just execute their blocks instantly when they are handed them, there's nothing to worry about in the first place, since it's only persistence that is the problem.
It sounds like you're just way over-thinking this.

How to pass int value from one class to another in xcode [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Hai I am new to xcode My code is below,
-(void)XYZ:(NSString *)id1 {
int id2 = [id1 intValue];
NSLog(#"%d",id2);
}
I need to use the id2 value in another class like below,
-(NSArray *)Vehicles {
NSString *urlString=[NSString stringWithFormat:#"http://www.xxxxxx.com/xxx_webservice/vehiclelist.php?uid=%d&format=json",id2];
}
Please guide me to pass the value thanks in advance...
First, you should read some docs about objective-c and OOP.
What you are calling classes, are methods. And if you want to pass one variable to a method, you have to declare it in the method declaration:
-(NSArray *)Vehicles:(NSInteger) id2 {
NSString *urlString=[NSString stringWithFormat:#"http://www.tranzlogix.com/tranzlogix_webservice/vehiclelist.php?uid=%d&format=json",id2];
}
And you have to pass it when you call it, if you are calling the method from your class, a correct way will be: [self Vehicles:id2]
But i suggest you to read a lot before asking so basic questions.

Calling a method from controller A in controller B in objective C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a method called someMethod in controller A and I want to call in controller B.
How would I do this?
Keep a pointer to your controllerA in your controllerB and do:
[self.controllerA performselector:#selector(someMethod) withObject:nil];
or just:
[self.controllerA someMethod];
But I agree with Shawn's comment, that this is not the best design - if you have functionality that needs to be shared between both view controllers, it might be better to move that functionality into a different class - your app delegate, maybe?
There are multiple ways of doing this. One is to create a class method. You create it like this:
+ (void) someMethod {
//Do stuff
}
Call it like this:
[ControllerB someMethod];
Another way to do it is to create a new instance of controller B as Jeff Loughlin said, and call the method on it:
ControllerB *cb = [[ControllerB alloc] init];
[cb someOtherMethod];

Resources