IOS development, ios development

Source: Internet
Author: User

IOS development, ios development

In actual development, we may need to add custom attributes to a file, or add information about the file to the attributes of the file, this can be used for the next read.

This article will introduce the "tool class for expanding file attributes"

 

Github URL also gives the example of this tool class source code: https://github.com/HeYang123456789/NSURLSession-ExpendedAttributesTool

 

This tool class design learning Source from Tan blog Note: http://www.tanhao.me/pieces/1102.html/ tan explains two ways to add properties for the local file property list, whereMethod 2 (a special AttributeName through NSFileManager)I found that it seems to be invalid. I am not sure why. I will try again later. However, if the method is effective, the copy of the Code is as follows:

 

ExpendFileAttributes. h

1 // 2 // ExpendFileAttributes. h 3 // NSURLSession implements offline resumable upload 4 // 5 // Created by HEYANG on 16/2/19. 6 // Copyright©2016 HEYANG. all rights reserved. 7 // 8 9/** 10 * ExpendFileAttributes tool class download source code: https://github.com/HeYang123456789/NSURLSession-ExpendedAttributesTool11 */12 13 # import <Foundation/Foundation. h> 14 15 @ interface ExpendFileAttributes: NSObject16 17/** adds an extension attribute to the file. The value is the string */18 + (BOOL) extendedStringValueWithPath :( NSString *) path key :( NSString *) key value :( NSString *) value; 19 20/** read the extended attributes of the file. The value is a string */21 + (NSString *) stringValueWithPath :( NSString *) path key :( NSString *) key; 22 23 24 @ end

ExpendFileAttributes. m

1 // 2 // ExpendFileAttributes. m 3 // NSURLSession implements offline resumable upload 4 // 5 // Created by HEYANG on 16/2/19. 6 // Copyright©2016 HEYANG. all rights reserved. 7 // 8 9/** 10 * ExpendFileAttributes tool class download source code: https://github.com/HeYang123456789/NSURLSession-ExpendedAttributesTool11 */12 13 # import "ExpendFileAttributes. h "14 15 # include <sys/xattr. h> 16 17 @ implementation ExpendFileAttributes18 // Add an extended attribute for the file 19 + (BOOL) extendedStringValueWithPath :( NSString *) path key :( NSString *) key value :( NSString *) stringValue20 {21 NSData * va Lue = [stringValue dataUsingEncoding: NSUTF8StringEncoding]; 22 ssize_t writelen = setxattr ([path fileSystemRepresentation], 23 [key UTF8String], 24 [value bytes], 25 [value length] 26 0, 27 0); 28 return writelen = 0? YES: NO; 29} 30 // Read File extended attributes 31 + (NSString *) stringValueWithPath :( NSString *) path key :( NSString *) key32 {33 ssize_t readlen = 1024; 34 do {35 char buffer [readlen]; 36 bzero (buffer, sizeof (buffer); 37 size_t leng = sizeof (buffer); 38 readlen = getxattr ([path fileSystemRepresentation], 39 [key UTF8String], 40 buffer, 41 leng, 42 0, 43 0); 44 if (readlen <0) {45 return nil; 46} 47 else if (readlen> sizeof (buffer) {48 continue; 49} else {50 NSData * data = [NSData dataWithBytes: buffer length: readlen]; 51 NSString * result = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; 52 NSLog (@ "result --- % @", result); 53 return result; 54} 55} while (YES); 56 return nil; 57} 58 @ end

Print the display result:

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.