Can't search files inside files from Dropbox - ios

I am trying to search any files using search display controller. But file inside files are not displaying in tableview. That means subfolder/files are not retrieved.
For getting file and folder I am using this code. I don't understand why subfiles are not displaying.
if(metadata.isDirectory)
{
marrFiles = [[NSMutableArray alloc]init];
for (DBMetadata *dbObject in metadata.contents)
{
if (!dbObject.isDirectory)
{
NSString *fileName = [dbObject.path lastPathComponent];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
NSString *stringSize = [NSString stringWithFormat:#"%#",[dbObject humanReadableSize]];
NSDate *modificationDate = [dbObject lastModifiedDate];
NSString *dateString = [dateFormatter stringFromDate:modificationDate];
dateString = [dateString createdTimeString];
[dict setValue:fileName forKey:#"File"];
NSString *stringInfo = [NSString stringWithFormat:#"%# %#",stringSize,dateString];
[dict setValue:stringInfo forKey:#"Size"];
[dict setValue:#"No" forKey:#"isSelected"];
[marrFiles addObject:dict];
}
else
{
NSString *fileName = [dbObject.path lastPathComponent];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setValue:#"No" forKey:#"isSelected"];
[dict setValue:fileName forKey:[NSString stringWithFormat:#"File"]];
[marrFiles addObject:dict];
}
}
[tblDropbox reloadData];
}
else
{
NSMutableArray *listFile=[NSMutableArray array];
for (DBMetadata *file in metadata.contents) {
[listFile addObject:file.filename];
}
}
[MBProgressHUD hideHUDForView:self.view animated:YES];

Related

How will i parse array data from nsdictionary.?

i tried to get image array from this json data. Please see my web data.
{"result":"Successful","data":{"id":"2","product_name":"6\" Round Plate","sku":"ECOW6RP","description":"Diameter 6.0 (inch) x Depth 0.6 (inch)\r\n\r\nPerfect for finger foods!","price":"42.89","business_price":"100.00","image":[{"image":"1454499251ecow6rp_01.jpg"}],"pack_size":"20","business_pack_size":"50","category":"2,3","tax_class":"1","created":"2016-01-19","altered":"2016-02-06 16:06:10","status":"1","deleted":"0","arrange":"1","delivery":"150.00"}}
i am getting every key value from this data by the following code.
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSDictionary *dictionary = [[NSJSONSerialization JSONObjectWithData:data options:0 error:nil]objectForKey:#"data"];
NSDictionary *arr = [[NSJSONSerialization JSONObjectWithData:data options:0 error:nil]objectForKey:#"image"];
NSLog(#"arrr %#",arr);
[productdetail removeAllObjects];
if (dictionary)
{
NSMutableDictionary *temp = [NSMutableDictionary new];
NSMutableDictionary *image = [NSMutableDictionary new];
[temp setObject:[dictionary objectForKey:#"product_name"] forKey:#"product_name"];
[temp setObject:[dictionary objectForKey:#"description"] forKey:#"description"];
[temp setObject:[dictionary objectForKey:#"pack_size"] forKey:#"pack_size"];
[temp setObject:[dictionary objectForKey:#"price"] forKey:#"price"];
[image setObject:[dictionary objectForKey:#"image"] forKey:#"image"];
// productdetail = [NSMutableArray arrayWithObjects:image,nil];
[productimage addObject:temp];
NSLog(#"productdetail %#", productdetail);
NSIndexPath *indexhpath;
_ProductName.text = [[productdetail objectAtIndex:indexhpath.row] objectForKey:#"product_name"];
_ProductDesc.text = [[productdetail objectAtIndex:indexhpath.row] objectForKey:#"description"];
_PackSize.text = [[productdetail objectAtIndex:indexhpath.row] objectForKey:#"pack_size"];
price = [[productdetail objectAtIndex:indexhpath.row] objectForKey:#"price"];
//Convert price into integer.
int Price = [price intValue];
NSInteger defaultpacks = 10;
value = _TotalPrice.text = [NSString stringWithFormat:#"%d", Price*defaultpacks];
NSLog(#"value %#", value);
firstincrease = [price intValue];
secondincrease = [value intValue];
NSString *str = [NSString stringWithFormat:#"%d",firstincrease+secondincrease];
number = [str intValue];
firstdecrease =[price intValue];
seconddecrease = [value intValue];
NSString *str2 = [NSString stringWithFormat:#"%d",firstincrease+secondincrease];
number2 = [str2 intValue];
}
if (productimage)
{
[_imagecollectionview reloadData];
}
}
i am getting image name from this data but not able to add product detail array in uicollection view. following error i get and crash the application. " reason: '-[__NSCFArray length]: unrecognized selector sent to instance 0x7b875590'
"
change this
[image setObject:[dictionary objectForKey:#"image"] forKey:#"image"];
into
NSMutableDictionary *temp = [NSMutableDictionary new];
NSMutableDictionary *image = [NSMutableDictionary new];
[temp setObject:[dictionary objectForKey:#"product_name"] forKey:#"product_name"];
[temp setObject:[dictionary objectForKey:#"description"] forKey:#"description"];
[temp setObject:[dictionary objectForKey:#"pack_size"] forKey:#"pack_size"];
[temp setObject:[dictionary objectForKey:#"price"] forKey:#"price"];
NSArray *ImageArray = [dictionary objectForKey:#"image"];
for (NSDictionary *imageDict in ImageArray)
{
[temp setObject:[imageDict objectForKey:#"image"] forKey:#"image"];
}
[productimage addObject:temp];

Sort array with dictionaries by date

I have an NSArray with NSDictionary inside. It looks like:
etc.
I need to sort it by date inside NSDictionary. I need something like this:
How can I do this? Here is my method which gives me first unsorted array:
- (void)iterateOverDocumentsDirectory
{
arrayWithFiles = [NSMutableArray new];
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirPath = [paths objectAtIndex:0];
NSString *finalPathToFolder = [NSString stringWithFormat:#"%#/", documentsDirPath];
albumNames = [[fileManager contentsOfDirectoryAtPath:finalPathToFolder error:&error] mutableCopy];
if (albumNames == nil) {
// error...
}
for (NSString *album in albumNames)
{
NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init];
[tempDict setValue:album forKey:#"name"];
NSString *finalPathToFiles = [NSString stringWithFormat:#"%#/%#", documentsDirPath, album];
NSArray *tempArray = [fileManager contentsOfDirectoryAtPath:finalPathToFiles error:&error];
NSMutableArray *arrayWithEachFiles = [NSMutableArray new];
for (NSString *tempString in tempArray)
{
NSMutableDictionary *eachFileDict = [[NSMutableDictionary alloc] init];
NSString *pathToFile = [NSString stringWithFormat:#"%#/%#/%#", documentsDirPath, album, tempString];
[eachFileDict setValue:pathToFile forKey:#"path"];
NSDictionary *filePathsArray1 = [[NSFileManager defaultManager] attributesOfItemAtPath:pathToFile error:nil];
NSDate *modifiedDate = [filePathsArray1 objectForKey:NSFileCreationDate];
[eachFileDict setValue:modifiedDate forKey:#"date"];
[arrayWithEachFiles addObject:eachFileDict];
}
NSSortDescriptor *ageDescriptor = [[NSSortDescriptor alloc] initWithKey:#"date" ascending:YES];
NSArray *sortDescriptors = #[ageDescriptor];
NSArray *sortedArrayWithFiles = [[arrayWithEachFiles sortedArrayUsingDescriptors:sortDescriptors] mutableCopy];
[tempDict setValue:sortedArrayWithFiles forKey:#"files"];
[arrayWithFiles addObject:tempDict];
}
}
Use following code for sorting the array of dictionaries:
NSArray * sortedArray = [myArray sortedArrayUsingComparator:^(id obj1, id obj2) {
NSNumber *rating1 = [(NSDictionary *)obj1 objectForKey:#"date"];
NSNumber *rating2 = [(NSDictionary *)obj2 objectForKey:#"date"];
return [rating1 compare:rating2];
}];
You can sort it easily by using sortArrayUsingComparator:
[array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return //pseudo-code: {obj1->dict->date isLaterThan obj2->dict->date};
}];
I sort it. Not sure that my code is clear and well optimized. But here is a solution:
NSMutableArray *masterArray = [NSMutableArray new];
NSMutableArray *sameDates = [NSMutableArray new];
BOOL started = NO;
NSString *prevDate;
for (NSDictionary *dict in sortedArrayWithFiles)
{
NSString *tempString = [dict objectForKey:#"date"];
if (started)
{
if ([tempString isEqualToString:prevDate])
{
[sameDates addObject:dict];
}
else
{
NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init];
[tempDict setValue:tempString forKey:tempString];
[tempDict setValue:sameDates forKey:#"files"];
[masterArray addObject:tempDict];
prevDate = tempString;
[sameDates removeAllObjects];
[sameDates addObject:tempString];
}
}
else
{
// first element
started = YES;
prevDate = tempString;
[sameDates addObject:dict];
}
}
// last value
NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init];
[tempDict setValue:prevDate forKey:prevDate];
[tempDict setValue:sameDates forKey:#"files"];
[masterArray addObject:tempDict];
In master array we store array with dictionaries separated by date.

Issues using isEqualToString: iOS

I'm trying to take an array that came from a plist, and use that strings to know if they already exists in the context.
But this error is happening
reason: '-[__NSCFArray isEqualToString:]: unrecognized selector sent to instance
here is the code..
- (NSArray*)loadPlist
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"AlreadyDownloaded" ofType:#"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
NSArray *plistArray = [[NSMutableArray alloc] initWithArray:[dict allValues]];
return plistArray;
}
-(void)writeToList:(NSArray *)text
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"AlreadyDownloaded" ofType:#"plist"];
NSArray *copy = [self loadPlist];
NSMutableArray *list = [copy mutableCopy];
[list addObjectsFromArray:text];
NSArray *array = [[NSArray alloc] initWithObjects:#"ObjectsIDS", nil];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
for(NSString *ob in list)
{
[dict setValue:ob forKey:#"ObjectsIDS"];
}
[dict writeToFile:path atomically:YES];
}
-(bool)compareIDS:(NSString *)text
{
NSArray *copy = [[NSArray alloc]initWithArray:[self loadPlist]];
NSMutableArray *list = [copy mutableCopy];
NSMutableString *st = [[NSMutableString alloc]init];
for(NSString *ob in copy)
{
if([ob isEqualToString:text])
return YES;
}
return NO;
}
Thank you guys!
Best Regards.
From the crash log we doubt the NSString *ob is an array, rather then a NSString, you'd better set a break point at for(NSString *ob in copy), the po the the object ob.
Use
NSArray indexOfObject Method:
return [copy indexOfObject:text] != NSNotFound
or
for(int i = 0; i < copy.count; i++) {
if([(NSString *) [copy objectAtIndex:i] isEqualToString:text]) return YES;
}
return NO;

Can't Read plist File

I am having trouble reading the data in a plist file. I'm not getting the titleString printed in the console like I'm expecting. What am I doing wrong?
NSString *path = [[NSBundle mainBundle] pathForResource:#"Events" ofType:#"plist"];
NSDictionary *dictPri = [[NSMutableDictionary alloc]initWithContentsOfFile:path];
NSMutableArray *arrEvents = [[NSMutableArray alloc] initWithArray:[dictPri valueForKey:#"Root"]];
for (NSDictionary *dict in arrEvents)
{
NSString *titleString = nil;
NSString *date = nil;
titleString = [NSString stringWithFormat:#"%#",[dict valueForKey:#"Title"]];
date = [NSString stringWithFormat:#"%#",[dict valueForKey:#"Date"]];
NSLog(#"Title String: %#", titleString);
}
Your main element (Root) is Dictionary - not Array - change it in plist by clicking on type next to it.
Also there is a problem in your code - you never access "Root" element by name - it's by default top-level object. Consider taking out additional array initialization which is not required.
Fixed code:
NSString *path = [[NSBundle mainBundle] pathForResource:#"Events" ofType:#"plist"];
NSArray* arrEvents = [NSArray arrayWithContentsOfFile:path];
for (NSDictionary *dict in arrEvents)
{
NSString *titleString = [NSString stringWithFormat:#"%#",[dict objectForKey:#"Title"]];
NSString *date = [NSString stringWithFormat:#"%#",[dict objectForKey:#"Date"]];
NSLog(#"Title String: %#", titleString);
}

Need help in trying to read and write from plist

I am trying to save an NSMutable Array and NSString into plist and then if it exists initialize the value from plist. However when I re run the app, the values do not get initialize as it is suppose to. So far the following is what I have.
if (self = [super init]) {
NSString *path=[self getFileUrl];
NSFileManager *fm = [[NSFileManager alloc] init];
if ([fm fileExistsAtPath:path]) {
_history = [d objectForKey:#"stringKey"];
class=[d objectForKey: #"ArrayKey"];
}
NSDictionary *d;
However the values are not getting initialized as per the plist. Is it the way I am extracting the values from the dictionary?
here is my function that saving json into plist in nscachesdirectory
-(void)saveproducts{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString* plistPath = [documentsPath stringByAppendingPathComponent:#"Products.plist"];
NSDictionary*dict = [NSDictionary dictionary];
NSMutableArray *types = [NSMutableArray array];
NSArray *dictArray = [NSArray array];
NSArray *dictKeys = [NSArray array];
NSArray *productObjects = [NSArray array];
NSArray *productKeys = [NSArray array];
for (int i=0; i<appDelegate.products.count; i++) {
NSMutableArray *tmpProds = [NSMutableArray array];
NSString *t_ID = [[appDelegate.products objectAtIndex:i] valueForKey:#"id"];
NSString *t_image = [[appDelegate.products objectAtIndex:i] valueForKey:#"image"];
NSString *t_name =[[appDelegate.products objectAtIndex:i] valueForKey:#"name"];
NSArray *products = [[appDelegate.products objectAtIndex:i] valueForKey:#"products"];
NSDictionary *productsDict = [NSDictionary dictionary];
for (int j=0; j<products.count; j++) {
NSString *p_id = [[products objectAtIndex:j] valueForKey:#"id"];
NSString *p_name = [[products objectAtIndex:j] valueForKey:#"name"];
NSString *p_name2 = [[products objectAtIndex:j] valueForKey:#"name2"];
NSString *image = [[products objectAtIndex:j] valueForKey:#"image"];
NSString *typeID = [[products objectAtIndex:j] valueForKey:#"type_id"];
NSString *active = [[products objectAtIndex:j] valueForKey:#"active"];
NSString *available = [[products objectAtIndex:j] valueForKey:#"available"];
NSString *desc = [[products objectAtIndex:j] valueForKey:#"description"];
NSString *price = [[products objectAtIndex:j] valueForKey:#"price"];
if ([p_name2 isEqual:[NSNull null]]) {
p_name2 =#"undefined";
}
if ([desc isEqual:[NSNull null]]) {
desc = #"";
}
productObjects = [NSArray arrayWithObjects:p_id,p_name,p_name2,image,typeID,active,available,desc,price, nil];
productKeys = [NSArray arrayWithObjects:#"id",#"name",#"name2",#"image",#"type_id",#"active",#"available",#"desc",#"price", nil];
productsDict = [NSDictionary dictionaryWithObjects:productObjects forKeys:productKeys];
[tmpProds addObject:productsDict];
if (![image isEqualToString:#""]) {
[foodImages addObject:image];
}
}
dictArray = [NSArray arrayWithObjects:t_ID,t_image,t_name,tmpProds, nil];
dictKeys = [NSArray arrayWithObjects:#"id",#"image",#"name",#"products", nil];
dict = [NSDictionary dictionaryWithObjects:dictArray forKeys:dictKeys];
[types addObject:dict];
}
NSDictionary* plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects:types, nil] forKeys:[NSArray arrayWithObjects:#"ptype", nil]];
NSString *error = nil;
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
if(plistData)
{
[plistData writeToFile:plistPath atomically:YES];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:YES forKey:#"cached"];
[defaults synchronize];
}
else
{
NSLog(#"Error log: %#", error);
}
}
and this one reading plist
-(void)loadFromplist{
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString* plistPath = [documentsPath stringByAppendingPathComponent:#"Products.plist"];
NSData *plistData = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSDictionary *dict = (NSDictionary*)[NSPropertyListSerialization propertyListFromData:plistData mutabilityOption:NSPropertyListMutableContainersAndLeaves format:nil errorDescription:nil];
productsArray = [dict valueForKey:#"ptype"];
[self.tableView reloadData];
}

Resources