Posts

Showing posts from March, 2018

Sort an NSMutablearray using custom objects in Objective C

Solution: Sort an NSMutablearray having two types.One is NSComparison Method and another one is NSSortdescriptor method. NSComparison Method: - ( NSComparisonResult ) compareArray :( NewMember *) otherObject { return [ self . birthDate compare : otherObject . birthDate ]; } NSArray * sortedArray = [ drinkDetails sortedArrayUsingSelector : @selector ( compareArray :)]; The array will be passed to another method with custom objects and return the greater object. NSSortdescriptor method: NSSortDescriptor * sortDesc ; sortDesc = [[ NSSortDescriptor alloc ] initWithKey :@ "birthDate" ascending : YES ]; NSArray * sortedArray = [ drinkDetails sortedArrayUsingDescriptors :@[ sortDesc ]]; The sortdescriptor method will be efficient method compare to previous one.

How to list all the files in the document directory?

Solution: The below code is used for print the document directory files that are stored based on the app. let fileManager = FileManager . default let nsDocumentDirectory = FileManager . SearchPathDirectory . documentDirectory let nsUserDomainMask = FileManager . SearchPathDomainMask . userDomainMask let paths = NSSearchPathForDirectoriesInDomains ( nsDocumentDirectory , nsUserDomainMask , true ) guard let dirPath = paths . first else { return } print(dirpath)

How to delete the document directory files?

Solution: The below function you can pass the filepath and it will delete the file in the directory.Because if you had used in your app once the folder or related data deleted then must delete the file it will be in cache.SO best to remove the data in the document directory.The app load will be high when not removing the files. Swift: func removeFile ( itemName : String ) { let fileManager = FileManager . default let nsDocumentDirectory = FileManager . SearchPathDirectory . documentDirectory let nsUserDomainMask = FileManager . SearchPathDomainMask . userDomainMask let paths = NSSearchPathForDirectoriesInDomains ( nsDocumentDirectory , nsUserDomainMask , true ) guard let dirPath = paths . first else { return } let filePath = "\(dirPath)/\(itemName)" do { try fileManager . removeItem ( atPath : filePath ) } catch let error as NSError { print ( error . debugDescription ) }} ObJective C: - ( void ) remov