The new iOS-NSDictionary/NSMutableDictionary uses "yes" and "no". The switch controller and NSUserDefaults are used to easily switch applications ., Iosnsuserdefaults

Source: Internet
Author: User

The new iOS-NSDictionary/NSMutableDictionary uses "yes" and "no". The switch controller and NSUserDefaults are used to easily switch applications ., Iosnsuserdefaults

IAronTalk Blog opens.

Not matter of the today will drag tomorrow.

-= -- =-= -= -=-

NSDictionary (dictionary) is a collection data used in Objective-C. It can store data as key-value pairs, when the value is set, you can directly obtain the corresponding value through the KEY, which is very convenient. The same dictionary object can store multiple different types of data. Unlike Java and C, only declared data of the same type can be saved.

As we all know, NSDictionary is a token for loading data, and "it" is still a switch. Let's take a look at how it makes its own features a "switch". The following example shows the "Switch" feature of NSDictionary. It must be noted that the dictionary initializes NSDictionary * switchDic = [[NSDictionary alloc] init]. Before you set the initial value, the dictionary content is empty.

Refer to the idea in this article. Many keywords can be used to complete the switch function, such as NSUserDefault. Compared with everyone, you still know the animation (slide view) used by each App for the first time ), the App used for the second time is missing. This function can be implemented with this idea. Please refer to the "report".

Example 1: (application background: Collect and expand the comment column)

  

# Import "BaseViewController. h "@ interface TopDetailViewController: BaseViewController <UITableViewDataSource, UITableViewDelegate> {UITableView * _ tableView; // The NSMutableDictionary * _ foldSwitch in the comment column; // The} @ end
@ Implementation TopDetailViewController-(void) viewDidLoad {[super viewDidLoad]; _ foldSwitch = [[NSMutableDictionary alloc] init];} # pragma mark-select cells to expand cells-(void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {NSString * key = [NSString stringWithFormat: @ "% d", indexPath. row]; // use the selected cell subscript as a dictionary key BOOL isShow = [[_ foldSwitch objectForKey: key] boolValue]; // The dictionary created is Null. isShow is NO. [_ foldSwitch setObject :@(! IsShow) forKey: key]; // after the cell is selected, the status of _ foldSwitch is changed. At this time, the value corresponding to the selected cell subscript key is: @ (YES ). // This line of code is particularly important. Refresh the selected row of cells to call the following method. [_ TableView reloadRowsAtIndexPaths: @ [indexPath] withRowAnimation: UITableViewRowAnimationFade];} # pragma-mark-method for setting the height of a cell in Table view-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath {// obtain the currently selected cell subscript, which may be the value of the dictionary key, in the previous protocol method, we set the value corresponding to the key in _ foldSwitch to @ (YES), so isSshow is YES NSString * key = [NSString stringWithFormat: @ "% I", indexPath. row]; BOOL isShow = [[_ foldSwitch objectForKey: key] boolValue]; if (isShow = YES) {// enter the expanded code here} else {// enter the collected code here} @ end

Example 2: (Application Scenario: When an App is used for the first time, a one-time display of the boot animation is displayed. When an App is opened later, the animation is not displayed)

 

  

1 # import "RootTabBarController. h "2 3 @ implementation RootTabBarController 4 5 // create a local storage object for small data 6 NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults]; 7 // obtain whether the current startup is the first time, if it is started for the first time, firstInit is NO. 8 BOOL firstInit = [[userDefaults objectForKey: @ "firstInit"] boolValue]; 9 if (firstInit = NO) {10 [self _ initFirstView]; // animation Loading Method 11 // change the value of firstInit to YES. The animation will not be loaded in the future. 12 [userDefaults setObject: @ YES forKey: @ "firstInit"]; 13 // It is recommended that the data be stored to the disk synchronously, but not must be 14 [userDefaults synchronize]; 15}

 

Due to the limited level of editors, the problem is inevitable. I urge all the experts to criticize and correct them and give valuable suggestions.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.