In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to use block, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Block syntax
1. The difference between proxy protocol and notification: the direct coupling of the two objects is different. The readability of the notification code is too poor. Agent, notification and block all realize the decoupling between objects. Notification communication is 1-to-many; agent, block is 1-to-1
2.block (closure): similar to a function, it is an anonymous function code that can be passed as an argument to other objects.
3. / *
Local basic data type variables, into the block, num will become constant. If you need to modify num in block, you need to add the keyword _ _ block (we can also modify it with the static keyword or set it to a global variable)
, /
_ _ blockint num = 10
MyBlock block1 = ^ {
NSLog (@ "num is d", num)
/ / there is an error when _ _ block is not added, and the constant cannot be self-added.
Num++
}
Num = 20
Block1 ()
NSLog (@ "num is d", num)
/ *
When a local OC object enters block, the object will be retain once, but not if modified with _ _ block (Note: block only acts as a retain when it is on the heap area, so here is a copy operation on block to copy the block from the stack area to the heap area)
, /
_ _ blockNSObject * obj = [[NSObjectalloc] init]
NSLog (@ "% ld", obj.retainCount)
MyBlock block2 = ^ {
NSLog (@ "% ld", obj.retainCount)
}
[block2 copy]; / / if you add a block copy to the heap area, it will no longer be affected by the stack area (then the Block copy will be allocated to the heap, so it is no longer restricted by the stack and can be used at will)
Block2 ()
_ _ blockPerson * ps = [[Personalloc] init]
MyBlock block3 = ^ {
NSLog (@ "% ld", ps.retainCount)
}
[block3 copy]
Block3 ()
[ps release]
[block3 release]
Memory Management of 4.Block
When an instance variable (member variable) is referenced in block, the object in which the variable resides will be retain once
/ / self-> btn-> block- > self causes circular reference and breaks block- > self
/ / _ blockSecondViewController * secondVC = self; creates secondVC as an intermediate variable for self (inside the SecondViewController class, and in the case of MRC)
/ / in ARC, the _ _ block keyword cannot solve the problem of circular reference, because even if the _ _ block keyword is used, it is still an object of type strong and will still be held by block when it enters the block block. At this time, the function of the _ _ block keyword is only to indicate that the pointer variable entering the block block is a modifiable variable. So use _ _ weak to create a weak reference relationship between the class and the object
_ _ weakSecondViewController * weakSelf = self
BaseButton * btn = [BaseButtonalloc] initWithFrame:CGRectMake (200,200,100,100) WithBlock: ^ (UIButton * btn) {
/ / normally, in the block block, we convert the _ _ weak object into a strong object to make it easier to get its own member variables
_ _ strongSecondViewController * strongSelf = weakSelf
[strongSelf.navigationControllerpopViewControllerAnimated:YES]
5. In the custom class BlockAlertView.m file
To copy the multi-parameter method of the parent class, the library function # import of C language needs to be introduced.
-(instancetype) initWithTitle: (NSString *) title message: (NSString *) message delegate: (id) delegatecancelButtonTitle: (NSString *) cancelButtonTitle otherButtonTitles: (NSString *) otherButtonTitles,...
{
Self = [superinitWithTitle:title message:message delegate:selfcancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles,nil]
/ / get multiple parameters after otherButtonTitles in multiple parameters
/ / define a pointer function
Va_list p
The next parameter pointed to by the pointer in the id test;// list
If (otherButtonTitles) {/ / if otherButtonTitles exists, traverses the list. If it does not exist, it is not multi-parameter and does not need to be traversed.
Va_start (p, otherButtonTitles)
While ((test = va_arg (p, id) {/ / if the parameter in the list is taken
[selfaddButtonWithTitle:test]; / / add parameters to AlertView and assign values to title
}
Va_end (p)
}
Returnself
}
In the controller RootViewController.m file, call the initialization multi-parameter method of the override parent class of BlockAlertView
BlockAlertView * alert = [[BlockAlertViewalloc] initWithTitle:@ "Welcome" message:@ "HelloWorld" delegate:selfcancelButtonTitle:@ "cancel" otherButtonTitles:@ "OK, @" exit ", @" No more prompt ", @" remind again ", nil]
Alert.block = ^ (NSInteger index) {
NSLog (@ "index----%ld", index)
}
[alert show]
Thank you for reading this article carefully. I hope the article "how to use block" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.