I have an iOS app where I am trying to display a list images found in a Dropbox folder, along with their modification date, in a table. The result would look something like this:
The following is successful in retrieving the file list, but I am stuck on getting file attributes as described in the Dropbox metadata API. I've tried several different ways to get this, as well as different properties. I get an error even before building.: Property 'modified' not found on object of type 'DBMetadata *'
- (void)restClient:(DBRestClient*)client loadedMetadata:(DBMetadata*)metadata {
NSArray* validExtensions = [NSArray arrayWithObjects:#"jpg", #"jpeg", nil];
NSMutableArray* newPhotoPaths = [NSMutableArray new];
NSMutableArray* fileModified = [[NSMutableArray alloc] init];
for (DBMetadata* child in metadata.contents)
{
NSString* extension = [[child.path pathExtension] lowercaseString];
if (!child.isDirectory && [validExtensions indexOfObject:extension] != NSNotFound)
{
[newPhotoPaths addObject:child.path];
NSLog(#"\t%#", newPhotoPaths);
//[fileModified addObject:child.modified]; // Error
//NSLog(#"%#", fileModified)
}
}
}
Suggestions greatly appreciated. Thanks.
I believe it is lastModifiedDate.
Related
I realize this question is pretty basic, but I'm really stuck. I have a plist. I'm trying to read that into an array so I can work with it in various classes. So in one class I have:
+ (NSArray*)questionArray
{
static NSArray* questions = nil;
if(!questions)
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"Questions" ofType:#"plist"];
NSDictionary *dic = [[NSDictionary alloc] initWithContentsOfFile:path];
NSArray *questionsArray = [dic objectForKey:#"groups"];
NSMutableArray *questionObjects = [[NSMutableArray alloc] initWithCapacity: [questionsArray count]];
for(NSDictionary* questionDic in questionsArray)
{
QuestionContainerObject* object = [[self alloc] initWithDictionary:questionDic];
[questionObjects addObject:object];
[object release];
}
questions = questionObjects;
[dic release];
}
return questions;
}
I want to be able to access the things I pull out of the array from another class. I tried calling it like NSString *str = [QuestionContainerObject questionArray]; from my other class (after importing the header) but I get the 'class method +questionArray not found' warning.
Can someone please point me in the right direction? I'm really lost! Thanks!!
The warning is because the compiler does not know the questionArray method exists.
define this method in the header (QuestionContainerObject.h)
#interface QuestionContainerObject
+ (NSArray*)questionArray;
#end
and in the file using it:
#import QuestionContainerObject.h
Also remember to release objects created using [class alloc]
Did you put the class method declaration in the header file?
Btw, move the static NSArray* questions = nil; outside of the method body and remove the = nil, or else its not going to have the effect you wanted, i think.
I am working on an iPhone app which involves using json-framework.I am getting array using NSURL
[{"firstName":"X","lastName":"Y","id":1},{"firstName":"A","lastName":"B","id":2}]
How can i get these 2 objects as like if i query for id=1, the O/P is
id=1
firstName:X
lastName:Y
and putting it in a table.
I am googling the stuff from many days but didn't get any solution.
Please help me out , explanation through code is appreciated.
Thank You.
If your target SDK is ios4 or higher, you can use this project
https://github.com/stig/json-framework/
Once you add the source to your project, just
#import "SBJson.h"
and convert your Json string as follows
jsonResponse = [string JSONValue];
The method will fail if you don't have the full Json array in your string, but you can keep appending strings until it doesn't fail
To follow up for codejunkie's request below
you can assume in your data structure that the jsonResponse is an NSArray
In other implementations take care to test the response for NSArray or NSDictionary
NSArray * myPeople = [string JSONValue];
NSMutableDictionary * organizedData = [[NSMutableDictionary alloc] init];
for (NSDictionary * p in myPeople) {
[organizedData setValue:p forKey:[p valueForKey:#"id"]];
}
// now you can query for an id like so
// [organizedData valueForKey:#"1"]; and your output will be what you wanted from the original question
// just don't forget to release organizedData when you are done with it
https://github.com/johnezang/JSONKit
I use this to get data from a webservice that spits out 50 records each having another 20 internal elements similar to the one you specify...
I use the JSONKit in the following manner..(Had a look at SBJson a lot of user but i got confused from the word go.)
JSONDecoder *jArray = [[JSONDecoder alloc]init];
NSMutableArray *theObject = [[NSMutableArray alloc] init];
theObject = [jArray objectWithData:theResponseData];//objectWithString:theResponseString
NSMutableArray *csArray = [[NSMutableArray array] retain] ;
for(id key in theObject)
{
if([key valueForKey:#"firstName"] != Nil)
{
........
}
if([key valueForKey:#"lastName"] != Nil)
{
........
}
}
check it out and let me know if it works or not.. By the way Great responses guys... Good
I'm writing an iPhone app that will use the EventKit framework to create new events in the user's Calendar. That part works pretty well (except for the wonky way it handles the timezone -- but that's another issue). What I can't figure out is how to get a list of the user's calendars so that they can choose which calendar to add the event to. I know that its an EKCalendar object but the docs don't show any way to get the whole collection.
Thanks in advance,
Mark
Searching through the documentation reveals an EKEventStore class that has a calendars property.
My guess is that you'd do something like:
EKEventStore * eventStore = [[EKEventStore alloc] init];
NSArray * calendars = [eventStore calendars];
EDIT: As of iOS 6, you need to specify whether you want to retrieve calendars of reminders or calendars of events:
EKEventStore * eventStore = [[EKEventStore alloc] init];
EKEntityType type = // EKEntityTypeReminder or EKEntityTypeEvent
NSArray * calendars = [eventStore calendarsForEntityType:type];
The code I used to get a useable NSDictionary of calendar names and types is like this:
//*** Returns a dictionary containing device's calendars by type (only writable calendars)
- (NSDictionary *)listCalendars {
EKEventStore *eventDB = [[EKEventStore alloc] init];
NSArray * calendars = [eventDB calendars];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
NSString * typeString = #"";
for (EKCalendar *thisCalendar in calendars) {
EKCalendarType type = thisCalendar.type;
if (type == EKCalendarTypeLocal) {
typeString = #"local";
}
if (type == EKCalendarTypeCalDAV) {
typeString = #"calDAV";
}
if (type == EKCalendarTypeExchange) {
typeString = #"exchange";
}
if (type == EKCalendarTypeSubscription) {
typeString = #"subscription";
}
if (type == EKCalendarTypeBirthday) {
typeString = #"birthday";
}
if (thisCalendar.allowsContentModifications) {
NSLog(#"The title is:%#", thisCalendar.title);
[dict setObject: typeString forKey: thisCalendar.title];
}
}
return dict;
}
I get the list of calendars OK - the problem is I don't get a user-displayable list. The calendar.title property is null for all of them; I don't see any kind of id property either.
-> Update: It works now for me. The mistake I had made was to put the eventStore object in a temporary variable, then get the list of calendars, then release the eventStore. Well if you do that all your calendars go away too. Containment is not strictly object oriented in some of the iOS frameworks, and this is an example of that. That is, the calendar object is dependent on the event store, it's not its own, separate entity.
Anyway -the solution above is fine!
I am creating an iPhone application which makes a iphone act as a pendrive for easy file sharing.
In the first stage, i have some files(png, pdf, jpg, zip) in a directory and i made them display in the tableview in the form of mutable array. It displays in the tableView as shown below,
.DS_Store
.localized
gazelle.pdf
Hamburger_sandwich.jpg
IITD TAJ Picture 028_jpg.jpg
iya_logo_final_b&w.jpg
manifesto09-eng.pdf
RSSReader.sql
SimpleURLConnections.zip
SQLTutorial
I just want to display the name of the files and i do not want to display the extensions. I know that it is possible to extract the extensions of a file in NSFileManager. But i do not know how. Please help me to make my table view look like this
.DS_Store
.localized
gazelle
Hamburger_sandwich
IITD TAJ Picture 028_jpg
iya_logo_final_b&w
manifesto09-eng
RSSReader
SimpleURLConnections
SQLTutorial
Look into NSString's documentation, there you'll find stringByDeletingPathExtension.
The basic idea is that you do not want to update objects in existing array, but create a new array with filenames without extension, and throw the original array away.
This is how it can be done:
NSArray *stuff = [NSArray arrayWithObjects:#"image1.jpg", #"image2.png", #"image3.tiff", nil];
NSLog(#"input %#", stuff);
stuff = [stuff valueForKey:#"stringByDeletingPathExtension"];
NSLog(#"output %#", stuff);
I do not expect you to understand that code, therefore here's a more straightforward version:
NSArray *stuff = [NSArray arrayWithObjects:#"image1.jpg", #"image2.png", #"image3.tiff", nil];
NSLog(#"input %#", stuff);
NSMutableArray *output = [NSMutableArray array];
for (NSString *filename in stuff) {
NSString *filenameWithoutExtension = [filename stringByDeletingPathExtension];
[output addObject:filenameWithoutExtension];
}
NSLog(#"output %#", output);
There have been a few threads on this topic but none have been able to solve my problem. Essentially I am trying to add a custom object to an NSMutableArray and it doesn't seem to be adding. I don't get any errors but I get a warning saying that my array is an "unused variable" so it looks like it is not getting used. See code below. Any help is appreciated!
Here is the initialization in the app delegate (on run time it says this array is not being used):
NSMutableArray *organArray = [[NSMutableArray alloc] init];
Here is my object class organ.m (I am importing the app delegate, the rootviewcontroller and the organ.h file)
Organ *organObj = [[Organ alloc] initWithPrimaryKey:primaryKey];
organObj.organName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,1)];
organObj.isDirty = NO;
[appDelegate.organArray addObject: organObj];
[organObj release];
I know the organObj.organName is getting the correct values from my sqlite db because I can output them to the console. They just don't seem to be getting added to the array and the fact that it says the array is not being used means something is wrong.
Thanks in advance
Just a guess but if organArray is intended to be a member of your app delegate, you are creating a new organArray when prefixing it with "NSMutableArray" so if I understand your code, change your app delegate to:
organArray = [[NSMutableArray alloc] init];
instead of:
NSMutableArray *organArray = [[NSMutableArray alloc] init];