Basic knowledge of IOS development-fragment 16, basic knowledge of ios-Fragment
1: dynamic type of Objective-C syntax (isKindOfClass, isMemberOfClass, id)
The ability of an object to obtain its type at runtime is called introspection. There are multiple methods to achieve this in introspection. Determine the object type-(BOOL) isKindOfClass: classObj determine whether it is an instance of this class or a subclass of this class-(BOOL) isMemberOfClass: classObj determine whether it is instance 1 of this class: person * person = [[Person alloc] init]; // parent class Teacher * teacher = [[Teacher alloc] init]; // subclass // YES if ([teacher isMemberOfClass: [Teacher class]) {NSLog (@ "teacher Teacher class member");} // NO if ([teacher isMemberOfClass: [Person class]) {NSLog (@ "member of the teacher Person class");} // NO if ([teacher isMemberOfC Lass: [NSObject class]) {NSLog (@ "member of the teacher NSObject class");} instance 2: 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 (@ "te Acher is a subclass of NSObject class or NSObject ");} isMemberOfClass determines whether it belongs to this type of instance and whether it is related to the parent class. Therefore, isMemberOfClass indicates NO when it reaches the parent class; determine the method:-(BOOL) respondsToSelector: selector to determine whether the instance has such a method + (BOOL) instancesRespondToSelector: To determine whether the class has this method. This method is a class method and cannot be used in object instance 3 of the class: // YES teacher is the object if ([teacher respondsToSelector: @ selector (setName:)] = YES) {NSLog (@ "teacher responds to setSize: method");} // YES Teacher is a class if ([Teacher instancesRespondToSelector: @ selector (teach)] = YES) {NSLog (@ "Teacher instance responds to teach method ");}
2: how to determine whether a string is a NULL Character in 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; } return NO;}
3. Delete the contents of the Caches folder.
// File Manager NSFileManager * mgr = [NSFileManager defaultManager]; // cache path NSString * caches = [Export (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 calculates the file or folder size. Because the osx Folder does not have a size, you need to calculate the subpathsAtPath from each file to obtain the folder. all files include * @ param filePath In the subfolders, such as the cache caches path * @ return size */-(NSInteger) fileSize :( NSString *) filePath {NSFileManager * mgr = [NSFileManager defaultManager]; // determine whether the file is BOOL dir = NO; BOOL exists = [mgr fileExistsAtPath: filePath isDirectory: & dir]; // The file \ Folder does not exist if (exists = NO) return 0; if (dir) {// self is a folder // traverse all contents in caches --- direct and indirect content NSArray * subpaths = [mgr subpathsAtPath: filePath]; NSInteger totalByteSize = 0; for (NSString * subpath in subpaths) {// obtain the full path NSString * fullSubpath = [filePath stringByAppendingPathComponent: subpath]; // determine whether the file is BOOL dir = NO; [mgr fileExistsAtPath: fullSubpath isDirectory: & dir]; if (dir = NO) {// file totalByteSize + = [[mgr attributesOfItemAtPath: fullSubpath error: nil] [NSFileSize] integerValue];} return totalByteSize;} else {// is a file. return [[mgr attributesOfItemAtPath: filePath error: nil] [NSFileSize] integerValue];} calls to input the following path: NSFileManager * mgr = [NSFileManager ultultmanager]; // cache path NSString * caches = [NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES) lastObject];
5: File Operations (NSFileManager) iOS ()
IOS sandbox mechanism. Applications can only access files in their app directories. Unlike android, iOS does not have an SD card and cannot directly access images, videos, and other content. Content generated by iOS apps, such as files and cached content, must be stored in your sandbox. By default, each sandbox contains three folders: Documents, Library, and tmp. The Library contains the Caches and Preferences directories. Documents: Apple recommends that you store the files created by the program and the file data generated by application browsing in this directory. This directory Library will be included during iTunes backup and recovery: the default setting or other status information of the storage program. Library/Caches: stores cached files and stores persistent data of the application. It is used to save the data after the application is upgraded or disabled, and is not synchronized by itunes, therefore, to reduce the synchronization time, you can consider storing some large files that do not need to be backed up in this directory. Tmp: provides a place to create temporary files in real time, but does not need to be persisted. After the application is closed, the data in this directory will be deleted, or the system may clear the data when the program is not running. A: Get the application sandbox root path:-(void) dirHome {NSString * dirHome = NSHomeDirectory (); NSLog (@ "app_home: % @", dirHome);} B: obtain the sources directory path:-(NSString *) dirDoc {// [NSHomeDirectory () stringByAppendingPathComponent: @ "events"]; NSArray * paths = logs (NSDocumentDirectory, NSUserDomainMask, YES ); NSString * documentsDirectory = [paths objectAtIndex: 0]; NSLog (@ "app_home_doc: % @", documentsDirect Ory); return documentsDirectory;} c: Obtain the Library directory path:-(void) dirLib {// [NSHomeDirectory () stringByAppendingPathComponent: @ "Library"]; NSArray * paths = require (NSLibraryDirectory, NSUserDomainMask, YES); NSString * libraryDirectory = [paths objectAtIndex: 0]; NSLog (@ "app_home_lib: % @", libraryDirectory);} d: obtain the Cache directory path:-(void) dirCache {NSArray * cacPath = NSSearchPathForDirect OriesInDomains (NSCachesDirectory, NSUserDomainMask, YES); NSString * cachePath = [cacPath objectAtIndex: 0]; NSLog (@ "app_home_lib_cache: % @", cachePath);} e: obtain the Tmp directory path:-(void) dirTmp {// [NSHomeDirectory () stringByAppendingPathComponent: @ "tmp"]; NSString * tmpDirectory = NSTemporaryDirectory (); NSLog (@ "app_home_tmp: % @ ", tmpDirectory);} f: Create a folder:-(void *) createDir {NSString * documentsPath = [self dirDoc]; NSFileManager * fileManager = [NSFileManager defaultManager]; NSString * testDirectory = [documentsPath stringByAppendingPathComponent: @ "test"]; // create directory BOOL res = [fileManager createDirectoryAtPath: testDirectory attributes: YES attributes: nil error: nil]; if (res) {NSLog (@ "folder created successfully");} else NSLog (@ "folder created failed");} g: create a file-(void *) createFile {NSString * documentsPath = [self dirDoc]; NSStrin G * 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 Created successfully: % @", testPath );} else NSLog (@ "file creation failed");} h: write data to the file:-(void) writeFile {NSString * DocumentsPath = [self dirDoc]; NSString * testDirectory = [documentsPath stringByAppendingPathComponent: @ "test"]; NSString * testPath = [testDirectory stringByAppendingPathComponent: @ "test.txt"]; NSString * content = @ "Test Written content! "; BOOL res = [content writeToFile: testPath atomically: YES encoding: NSUTF8StringEncoding error: nil]; if (res) {NSLog (@" file written successfully ");} 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 stringByAppendingPath Component: @ "test"]; NSFileManager * fileManager = [NSFileManager defaultManager]; NSString * testPath = [testDirectory attributes: @ "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 a file: -(void) deleteFile {NSString * documentsPath = [self dirDoc]; NSString * testDirectory = [documentsPath stringByAppendingPathComponent: @ "test"]; NSFileManager * fileManager = [NSFileManager defamanager manager]; NSString * testPath = [testDirectory stringByAppendingPathComponent: @ "test.txt"]; BOOL res = [fi LeManager removeItemAtPath: testPath error: nil]; if (res) {NSLog (@ "File deleted successfully");} else NSLog (@ "File deleted failed "); NSLog (@ "does the file exist: % @", [fileManager isExecutableFileAtPath: testPath]? @ "YES": @ "NO ");}