IOS-basic development knowledge

Source: Internet
Author: User

IOS-basic development knowledge

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];

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.