Showing posts with label File. Show all posts
Showing posts with label File. Show all posts

Friday, September 14, 2018

fileprivate to private


In Swift 3, If we need to access private stuff of a class in extension, we need to change the access specifier from private to fileprivate.


     
  class User {
    
    private var name: String
    private var id: Int
    
    init(name: String, id: Int) {
        self.name = name
        self.id = id
    }
    
    private var greeting: String {
        get {
            return "Hi \(name)"
        }
        set(v) {
            name = v
        }
    }
    
 }

 extension User {
    func greetUser() {
        print(greeting)
    }
 } 



Swift 3 gives build error for below as greeting is private. 



     
  extension User {
    func greetUser() {
        print(greeting)
    }
  }



It should be marked as fileprivate which makes it accessible to others which are inside a file which may not be we wanted most of the times.

Swift 4, makes it appropriate based on their name.

private is not at all accessible outside even with in the same file.

fileprivate is accessible only with in the file, Not accessible outside of a file.

If greeting is with in the same file where User class is there, It doesn’t matter If it’s fileprivate or private to access it in a User extension with in the same file. But, careful with fileprivate cause it is accessible with in the same file. 


     
  class User {
    
    private var name: String
    private var id: Int
    
    init(name: String, id: Int) {
        self.name = name
        self.id = id
    }
    
    fileprivate var greeting: String {
        get {
            return "Hi \(name)"
        }
        set(v) {
            name = v
        }
    }
    
 }

 extension User {
    func greetUser() {
        print(greeting)
    }
 }



If you are writing extension of User in another file, You can not access it If it’s fileprivate or private.

Extension outside of file,


     
  extension User {
    func greetUser() {
        print(greeting)
    }
  }



fileprivate build error - 'greeting' is inaccessible due to 'fileprivate' protection level

private build error - 'greeting' is inaccessible due to 'private' protection level


That’s how Swift 4, make both the access specifiers clear based on their name. 

Glad, It’s not confusing now like in Swift 3.

Swift 3, fileprivate -  If you need access throughout the file even in extensions (private gives build error in extensions)

Swift 3, private - Not accessible outside the model scope even in the extension which is there in same file.

Swift 4, fileprivate - If you need access throughout the file.

Swift 4, private - Accessible in extensions If they are in the same file. Not accessible for all remaining cases.

Take Aways - 

In Swift 4, We don’t need to change fileprivate access for a property/method to private to access that in the extension in the same file.


Hope this post is useful. Feel free to comment incase of any queries.


Tuesday, September 1, 2015

Objective C BOX API updating File/Folder tags

Objective C BOX API doesn't directly give you an option to update file/folder tags. Where as it gives option to update name and description of the items which are very important compared to tags. Through out this article If I say BOX item, that means whether a BOX file or folder.

In this article I am going to explain, how we can edit BOX API classes to achieve this functionality like updating tags of a file or folder. Before going to that, let's have a look at how we can edit the name of a BOX item (file/folder). Below is the sample code I have used in my application to rename a BOX item.


File Rename:

 BoxFilesRequestBuilder *fileRequestBuilder = [[BoxFilesRequestBuilder allocinitWithQueryStringParameters:@{@"fields" : @"name,type,description,tags,id,size,created_at,created_by,parent"}];

 fileRequestBuilder.name = fileName;    
    [[BoxSDK sharedSDK].filesManager editFileWithID:fileID requestBuilder:fileRequestBuilder success:^(BoxFile *file) {
        resultBlock(file,nil);      
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, NSDictionary *JSONDictionary) {
        resultBlock(nil,error);
    }];


Folder Rename:


    BoxFoldersRequestBuilder *folderBuilder = [[BoxFoldersRequestBuilder alloc] initWithQueryStringParameters:@{ @"fields" : @"name,type,description,tags,id,size,created_at,created_by,parent"}];
    folderBuilder.name = folderName;
    
    [[BoxSDK sharedSDK].foldersManager editFolderWithID:folderID requestBuilder:folderBuilder success:^(BoxFolder *folder) {
        resultBlock(folder,nil);
        
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, NSDictionary *JSONDictionary) {
        resultBlock(nil,error);

    }];




We use BoxFilesRequestBuilder to request updates on BOX files and BoxFoldersRequestBuilder to request updates on BOX folder items. We give this request to filesManger of BoxSDK for files and foldersManager of BoxSDK for folders to get the update done BOX items.

If you look at the above code for name update, there is a property called 'name' in the BoxFoldersRequestBuilder & BoxFilesRequestBuilder. But there is no property called 'tags'. So, all we need to do here is add property called 'tags' to these classes.


  • Add the following line in BoxItemsRequestBuilder.h file, which is the base class for both files and folders request builder classes.



                   @property (nonatomic, readwrite, strongNSArray *tags;

  • And the below three steps of code in the BoxItemsRequestBuilder.m file.



1. Before init method:


@synthesize tags = _tags;      

2. In initWithQueryStringParameters method:


_tags = nil;   

3. bodyParameters method:        


[self setObjectIfNotNil:self.tags forKey: @"tags" inDictionary:dictionary];  



Once you place this code, we will get options to update tags for BOX items as shown below.

fileRequestBuilder.tags = tagsArr;    
folderBuilder.tags = tagsArr;


Hope this article is useful to iOS developers using BOX API.

Feel free to comment in case of any queries.concerns.