1. Project
Create a new Bookmark Manager project, you can store the bookmark URL, Chinese name, star rating, access and permission information, with the function of adding, deleting, changing, checking and sorting.
2. Find objects, abstract classes
Bookmark Manager, bookmark manager, bookmark Manager ... Read a few more times to find the objects, bookmarks and manager. Bookmarks contain information such as URL, Chinese name and star rating, and the organizer has bookmarks. Then I can abstract two classes, one is a bookmark class, and one is the manager class.
We add, delete, change, check and sort bookmarks in the bookmark Manager, and save the bookmark's attribute data in the bookmark.
3. Object properties and Behavior analysis
Analysis of the storage relationships of the bookmark manager:
4. Implementation of Classes
1. Implementation of the Bookmark class
#import <foundation/foundation.h>//Bookmark class to store bookmarks specific information/data @interface bookmark:nsobject{nsstring * _URL; URL NSString * _title; Title Nsuinteger _stars; Star Nsuinteger _vist; Number of visitors char _level; Permissions}-(void) SetUrl: (NSString *) url;-(void) Settitle: (NSString *) title;-(void) Setstars: (Nsuinteger) stars;-(void) Setvist: (Nsuinteger) vist;-(void) SetLevel: (char) level;-(NSString *) geturl;-(NSString *) gettitle;-(Nsuinteger) getstars;-(Nsuinteger) getvist;-(char) getlevel;//construction Method stores data-(Instancetype) Initwithurl: (nsstring *) URL Withtitle: (NSString *) title withsarts: (Nsuinteger) stars withvist: (nsuinteger) vist withlevel: (char) level; @end
#import "BookMark.h" @implementation bookmark-(Instancetype) Initwithurl: (nsstring *) URL withtitle: (NSString *) title Withsarts: (Nsuinteger) stars Withvist: (Nsuinteger) vist withlevel: (char) level{self = [Super Init];if (self) {_url = Url;_ title = Title;_stars = Stars;_vist = Vist;_level = level;} return self;} -(void) SetUrl: (NSString *) Url{_url = URL;} -(void) Settitle: (NSString *) Title{_title = title;} -(void) Setstars: (nsuinteger) stars{_stars = stars;} -(void) Setvist: (nsuinteger) vist{_vist = vist;} -(void) SetLevel: (char) level{_level = level;} -(NSString *) Geturl{return _url;} -(NSString *) Gettitle{return _title;} -(Nsuinteger) Getstars{return _stars;} -(Nsuinteger) Getvist{return _vist;} -(char) Getlevel{return _level;} @end
2. Implementation of the manager class
#import <Foundation/Foundation.h> #import "BookMark.h"//Bookmark Manager Class @interface Bookmanager:nsobject{nsmutablearray * _array;} -(instancetype) init;//Delete-(void) Removebookmark: (NSString *) title;//Add-(void) Addbookmarkwithurl: (nsstring *) URL wi Thtitle: (NSString *) title Withstars: (Nsuinteger) stars Withvist: (Nsuinteger) vist withlevel: (char) level;//Modify-(V OID) Modifytitle: (NSString *) title Withnewtitle: (NSString *) newtitle;//Lookup-(void) Searchwithtitle: (NSString *) title;- (void) sortwithstars;-(void) Showallbookmark; @end
#import "BookManager.h" @implementation bookmanager-(instancetype) init{self = [Super Init];if (self) {_array = [[ Nsmutablearray alloc] init];} return self;} Add Bookmark-(void) Addbookmarkwithurl: (nsstring *) URL withtitle: (NSString *) title Withstars: (Nsuinteger) stars Withvist: (N Suinteger) vist withlevel: (char) level{for (int i = 0; i < [_array count]; i + +) {if ([title Isequaltostring:[_array[i] GetTitle]] {NSLog (@ "repeat add"); return;}} The following can either use the constructor method or the set method to BookMark *book = [[BookMark alloc] Initwithurl:url withtitle:title withsarts:stars withvist: Vist withlevel:level];//bookmark * book = [[BookMark alloc] Init];//[book seturl:url];//[book Settitle:title];//[book Setstars:stars];//[book Setvist:vist];//[book Setlevel:level]; [_array Addobject:book];} Delete bookmark-(void) Removebookmark: (NSString *) title{for (int i = 0; i < [_array count]; i + +) {if ([title isequaltostring:[ _array[i] GetTitle]) {[_array Removeobject:_array[i]];return;} else NSLog (@ "Without this Bookmark");}} Modify Bookmark-(void) Modifytitle: (nsstring *) title Withnewtitle: (NSString *) newtitle{for (int i = 0; i < [_array count]; i + +) {if ([title Isequaltostring:[_arra Y[i] GetTitle]) {[_array[i] settitle:newtitle];}}} Find bookmark-(void) Searchwithtitle: (NSString *) Title{int index =-1; Save Bookmark Location//index = 1 indicates that no corresponding bookmark was found//traversal of the bookmark, and after locating it, save the target bookmark position and exit the loop for (int i = 0; i < [_array count]; i + +) {if ([title Isequaltos Tring:[_array[i] GetTitle]) {index = I;break;}} if (index = =-1) {NSLog (@ "did not find a bookmark to find");} else NSLog (@ "found the bookmark pos =%d", index);} -(void) sortwithstars{//the bookmark object with a bubbling sort for (int i = 0; i < [_array count]-1; i + +) {//control the number of rounds for the sort for (int j = 0; J < ; [_array Count]-1-i; J + +) {//control comparison of the number of times if ([_array[j] getstars] > [_array[j + 1] getstars]) {Nsarray * temp;temp = _array[j];_array[j] = _arr Ay[j + 1];_array[j + 1] = temp;}}} -(void) showallbookmark{for (int i = 0; i < [_array count]; i + +) {NSLog @ "URL:%@ title:%@ Star:%ld Visits:%ld permissions:%c", [_array [i] getUrl], [_array[i] getTitle], [_array[i] getstars], [_array[i] getvist], [_arRay[i] getlevel]);}} @end
5. Actions of the Bookmark manager
In this step we imagine that a person is manipulating a bookmark manager with an interface, and because there is no interface, we can only operate inside the main function. For example:
#import <Foundation/Foundation.h> #import "BookMark.h" #import "BookManager.h" int main (int argc, const char * argv[ ] {@autoreleasepool {//Create a bookmark for baidu bookmanager* mybookmanager = [[Bookmanager alloc] init];[ Mybookmanager addbookmarkwithurl:@ "www.baidu.com" withtitle:@ "Baidu" Withstars:3 withvist:123 withlevel: ' A '];// Create a new Sina bookmark [Mybookmanager addbookmarkwithurl:@ "www.sina.com" withtitle:@ "Sina" withstars:7 withvist:456 withlevel: ' B '] ;//Create a new Sohu bookmark [Mybookmanager addbookmarkwithurl:@ "www.souhu.com" withtitle:@ "Sohu" Withstars:5 withvist:756 withlevel: ' C '];//repeat add [Mybookmanager addbookmarkwithurl:@ "www.souhu.com" withtitle:@ "Sohu" Withstars:5 withvist:756 withlevel: ' C ' ];//Show all bookmarks NSLog (@ "bookmark all added"); [Mybookmanager showallbookmark];//Find [Mybookmanager searchwithtitle:@ "Sina"];//delete [Mybookmanager removeBookMark:@ "Baidu" ]; NSLog (@ "deleted bookmarks"); [Mybookmanager showallbookmark];//sort [Mybookmanager sortwithstars]; NSLog (@ "sorted bookmarks"); [Mybookmanager showallbookmark];//Modified [Mybookmanager modifytitle:@ "Sina" withnewtitle:@ "123"]; NSLog (@ "modified bookmarks"); [Mybookmanager Showallbookmark];} return 0;}
First of all, we have three bookmarks: Baidu, Sina and Sohu. The content of the bookmark is the contents of the program.
We then verify that adding bookmarks, finding bookmarks, deleting bookmarks, sorting bookmarks, and modifying bookmarks are the correct way to behave.
The output of the final program is:
2015-07-22 20:15:58.560 3. Bookmark Manager [3,083:303] Repeat add 2015-07-22 20:15:58.562 3. Bookmark Manager [3,083:303] bookmark all added 2015-07-22 20:15:58.563 3. Bookmark Manager [3,083:303] URL: www.baidu.com title: Baidu Star: 3 visits: 123 Permissions: A2015-07-22 20:15:58.565 3. Bookmark Manager [3,083:303] Website: www.sina.com title: Sina Star: 7 Visits: 456 permissions: B2015-07-22 20:15:58.565 3. Bookmark Manager [3,083:303] URL: www.souhu.com title: Sohu Star: 5 Visits: 756 Permissions: C 2015-07-22 20:15:58.566 3. Bookmark Manager [3,083:303] found the bookmark pos = 12015-07-22 20:15:58.568 3. Bookmark Manager [3,083:303] bookmark after delete 2015-07-22 20:15:58.568 3. Bookmark Manager [3,083:303] URL: www.sina.com title: Sina Star: 7 Visits: 456 permissions: B2015-07-22 20:15:58.569 3. Bookmark Manager [3,083:303] Website: www.souhu.com title: Sohu Star: 5 Visits: 756 permissions: C2015-07-22 20:15:58.570 3. Bookmark Manager [3,083:303] after sorting 2015-07-22 20:15:58.571 3. Bookmark Manager [3,083:303] Website: www.souhu.com title: Sohu Star: 5 Visits: 756 permissions: C2015-07-22 20:15:58.573 3. Bookmark Manager [3,083:303] URL: www.sina.com title: Sina Star: 7 visit Q: 456 permissions: B2015-07-22 20:15:58.574 3. Bookmark Manager [3,083:303] modified bookmarks 2015-07-22 20:15:58.576 3. Bookmark Manager [3,083:303] URL: www.souhu.com Title: Sohu Star: 5 Visits: 756 permissions: C2015-07-22 20:15:58.577 3. Bookmark Manager [3,083:303] URL: Www.sina. com title: 123 stars: 7 visits: 456 permissions: Bprogram ended with exit code:0
As we expected, the project features of our Bookmark Manager are implemented.
6. Summary
In this bookmark manager we have the following points to learn:
1. Analysis of project requirements;
2. Find the object, abstract out the class, analyze the properties and behavior of the class;
3. Implementation class;
4. Verify that the relevant behavior or method is correct.
Bookmark Manager is stored in the bookmark, the bookmark is stored in the data. Here I set the behavior of a mutable array in the Bookmark manager to store the address of the bookmark. Each time we add a bookmark, we create a bookmark object and write the data in the bookmark. When you delete a bookmark, you delete the bookmark's position in the array.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"iOS development objective-c" bookmark Manager Project