iOS development--Summarize &ios development Basics

Source: Internet
Author: User

iOS Development Basics

Dynamic type of 1:objective-c syntax (Iskindofclass, Ismemberofclass,id)

The ability of an object to acquire its type at run time is called introspection. Introspection can be accomplished in a number of ways. Determine the object type-(BOOL) iskindofclass:classobj whether this class or subclass of this class is an instance of (bool) Ismemberofclass:classobj to determine if it is an instance instance of this class: person *      person = [[Person alloc] init];  Parent class Teacher *teacher = [[Teacher alloc] init];     Subclass//yes if ([Teacher Ismemberofclass:[teacher class]]) {NSLog (@ "Member of teacher Teacher Class");     }//no if ([Teacher Ismemberofclass:[person class]]) {NSLog (@ "Teacher Member of Person class");     }//no if ([Teacher Ismemberofclass:[nsobject class]]) {NSLog ("Member of Teacher NSObject class");  } instance two: Person *person = [[Person alloc] init];    Teacher *teacher = [[Teacher alloc] init];  Yes if ([Teacher Iskindofclass:[teacher class]]) {NSLog (@ "Teacher is a subclass of teacher class or teacher");  }//yes if ([Teacher Iskindofclass:[person class]]) {NSLog (@ "Teacher is a subclass of the person class or person");  }//yes if ([Teacher Iskindofclass:[nsobject class]]) {NSLog (@ "Teacher is a subclass of NSObject class or nsobject"); } Ismemberofclass to determine if it belongs to this type ofinstance, whether it is related to the parent class, so Ismemberofclass refers to the parent class will be no; determine the method:-(BOOL) Respondstoselector:selector Interpretation instance if there is such a method + (BOOL) Instancesrespondtoselector: Determines whether the class has this method. This method is a class method that cannot be used in an object instance of a class three:/Yes teacher is an object if ([Teacher Respondstoselector: @selector (setName:)] = = yes) {NSLog (@ "Te  Acher responds to Setsize:method "); }//Yes Teacher is class if ([Teacher instancesrespondtoselector: @selector (teach)] = = yes) {NSLog (@ "Teacher instance R  Esponds to teach method ");   }

A method of determining whether a string is null in 2:ios development

-(BOOL) isblankstring: (NSString *) string {    if (string = = Nil | | string = NULL) {        return YES;    }    if ([String Iskindofclass:[nsnull class]]) {        return YES;    }    if ([[[String Stringbytrimmingcharactersinset:[nscharacterset Whitespacecharacterset]] length]==0) {        return YES;    }    

3: Delete the contents of the caches folder

File Manager Nsfilemanager *mgr = [nsfilemanager defaultmanager];//cache path NSString *caches = [ Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) lastobject]; [Mgr Removeitematpath:caches Error:nil];

4: Calculate the size of a folder or file

/** * @ 15-06-17 09:06:22 * * @brief calculate the size of the file or folder because the OSX folder is no size This property to be calculated by each file Subpathsatpath can get the folder underneath all the files containing subfolders inside * @param filePath such as the path of the cache caches * @return size */-(Nsinteger) fileSize: (NSString *) filepath{nsfilemanager *mgr = [Nsfil    Emanager Defaultmanager];    Determines whether the file is BOOL dir = NO;    BOOL exists = [Mgr Fileexistsatpath:filepath isdirectory:&dir];        File \ folder does not exist if (exists = = no) return 0;        if (dir) {//Self is a folder//traversal of all content inside caches---Direct and indirect content nsarray *subpaths = [Mgr Subpathsatpath:filepath];        Nsinteger totalbytesize = 0; For (NSString *subpath in subpaths) {//Get full path nsstring *fullsubpath = [FilePath Stringbyappendingpa            Thcomponent:subpath];            Determines whether the file is BOOL dir = NO;            [Mgr Fileexistsatpath:fullsubpath isdirectory:&dir]; if (dir = = NO) {//file Totalbytesize + = [[Mgr Attributesofitematpath:fullsubpath error:nil][nsfilesize] int     Egervalue];       }} return totalbytesize;    } else {//is a file return [[Mgr Attributesofitematpath:filepath error:nil][nsfilesize] integervalue]; }} call passed in the following path: Nsfilemanager *mgr = [nsfilemanager defaultmanager];//cache path NSString *caches = [ Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) lastobject];

5: File Operations (Nsfilemanager) IOS (go)

The sandbox mechanism for iOS, apps can only access files in their own app directory. Unlike Android, iOS does not have an SD card concept and does not have direct access to images, videos, and other content. Content generated by iOS apps, like, files, cached content, and so on, must be stored in their own sandbox. By default, each sandbox contains 3 folders: Documents, Library, and TMP. The library contains caches, preferences directories. Documents: Apple recommends that the files generated by the program creation and the file data generated by the app's browsing be kept in this directory, and itunes backup and recovery will include this directory library: default settings or other status information for the stored program; library/ Caches: Store the cache file, save the persisted data of the application, use it to apply the upgrade or save the data after the app is closed, and not be synced by itunes, so in order to reduce the time of synchronization, consider putting some larger files into this directory without needing backup. TMP: Provides an immediate place to create temporary files, but does not need to be persisted, after the application is closed, the data in the directory will be deleted, or the system may be cleared when the program is not running.          A: Get app sandbox root path:-(void) dirhome{nsstring *dirhome=nshomedirectory ();  NSLog (@ "App_home:%@", dirhome);      } B: Get the Documents directory path:-(NSString *) dirdoc{//[nshomedirectory () stringbyappendingpathcomponent:@ "Documents"];      Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);      NSString *documentsdirectory = [Paths objectatindex:0];      NSLog (@ "App_home_doc:%@", documentsdirectory);  return documentsdirectory; } C: Get library directory path:-(void) dirlib{//[nshomedirectory () stringbyappendingpathcomponent:@ "LIbrary "];      Nsarray *paths = Nssearchpathfordirectoriesindomains (Nslibrarydirectory, Nsuserdomainmask, YES);      NSString *librarydirectory = [Paths objectatindex:0];  NSLog (@ "App_home_lib:%@", librarydirectory); } D: Get the cache directory path:-(void) dircache{Nsarray *cacpath = Nssearchpathfordirectoriesindomains (Nscachesdirectory, NSUserDom      Ainmask, YES);      NSString *cachepath = [Cacpath objectatindex:0];  NSLog (@ "App_home_lib_cache:%@", CachePath);      } e: Get tmp directory path:-(void) dirtmp{//[nshomedirectory () stringbyappendingpathcomponent:@ "TMP"];      NSString *tmpdirectory = Nstemporarydirectory ();  NSLog (@ "app_home_tmp:%@", tmpdirectory);      } F: Create folder:-(void *) createdir{nsstring *documentspath =[self Dirdoc];      Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];      NSString *testdirectory = [Documentspath stringbyappendingpathcomponent:@ "test"]; Create directory BOOL Res=[filemanager createdirectoryatpath:testdirectory withintermediatedirectories:yes attrIbutes:nil Error:nil];      if (res) {NSLog (@ "folder creation succeeded");   }else NSLog (@ "folder creation failed");      } g: Create File-(void *) createfile{nsstring *documentspath =[self Dirdoc];      NSString *testdirectory = [Documentspath stringbyappendingpathcomponent:@ "test"];      Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];      NSString *testpath = [testdirectory stringbyappendingpathcomponent:@ "test.txt"];      BOOL Res=[filemanager Createfileatpath:testpath Contents:nil Attributes:nil];      if (res) {NSLog (@ "File creation succeeded:%@", Testpath);  }else NSLog (@ "File creation failed");      } h: Write data to File:-(void) writefile{nsstring *documentspath =[self Dirdoc];      NSString *testdirectory = [Documentspath stringbyappendingpathcomponent:@ "test"];      NSString *testpath = [testdirectory stringbyappendingpathcomponent:@ "test.txt"]; NSString *[email protected] "Test write content!      ";     BOOL res=[content writetofile:testpath atomically:yes encoding:nsutf8stringencoding Error:nil]; if (res) {NSLog (@ "file write succeeded");  }else NSLog (@ "file write Failed");      } I: Read file data:-(void) readfile{nsstring *documentspath =[self Dirdoc];      NSString *testdirectory = [Documentspath stringbyappendingpathcomponent:@ "test"];  NSString *testpath = [testdirectory stringbyappendingpathcomponent:@ "test.txt"];  NSData *data = [NSData Datawithcontentsoffile:testpath];      NSLog (@ "file read succeeded:%@", [[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding]);      NSString *content=[nsstring Stringwithcontentsoffile:testpath encoding:nsutf8stringencoding Error:nil];  NSLog (@ "file read succeeded:%@", content);      } J: File attribute:-(void) fileattriutes{nsstring *documentspath =[self Dirdoc];      NSString *testdirectory = [Documentspath stringbyappendingpathcomponent:@ "test"];      Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];      NSString *testpath = [testdirectory stringbyappendingpathcomponent:@ "test.txt"]; Nsdictionary *fileattributes = [FileManager attribUtesofitematpath:testpath Error:nil];      Nsarray *keys;      ID key, value;      Keys = [FileAttributes AllKeys];      int count = [keys count];          for (int i = 0; i < count; i++) {key = [keys objectatindex:i];          Value = [FileAttributes Objectforkey:key];      NSLog (@ "key:%@ for Value:%@", key, value);      }} k: delete file:-(void) deletefile{nsstring *documentspath =[self Dirdoc];      NSString *testdirectory = [Documentspath stringbyappendingpathcomponent:@ "test"];      Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];         NSString *testpath = [testdirectory stringbyappendingpathcomponent:@ "test.txt"];      BOOL Res=[filemanager Removeitematpath:testpath Error:nil];      if (res) {NSLog (@ "file deletion succeeded");         }else NSLog (@ "File deletion failed");  NSLog (@ "file exists:%@", [FileManager isexecutablefileatpath:testpath][email protected] "yes": @ "NO");   }

iOS development--Summarize &ios development Basics

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.