How to store .mp3 file into Core Data? - core-data

I found this code to save the file on disk:
char *saves = "abcd";
NSData *data = [[NSData alloc] initWithBytes:saves length:4];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:#"MyFile"];
[data writeToFile:appFile atomically:YES];
I am new to Core Data. I want to store MP3 files using it. First I want to download it from web URL, then save it in core data.

You should save the files in the application documents directory (perhaps create a "music" subfolder), and store a reference to those files in Core Data. Ensure that these file are named uniquely (NSUUID may be useful) and that you don't store the path to the file on your managed object, since this is not guaranteed to be the same next time the application runs.

Related

Fix error "initializer element is not a compile-time constant"

I'm trying to create a tweak for iOS, but when I'm going to compile the tweak, I get this error:
"initializer element is not a compile-time constant".
//Sound path
NSString *soundPath = [[NSBundle bundleWithPath:#"/Library/Application Support/joselito/"] pathForResource:#"joselito" ofType:#"caf"];
NSURL *soundURL = [[NSURL alloc] initFileURLWithPath:soundPath];

NSString showing Null

I am trying to access a core data model stored in another view controller. Currently, I have a few strings that i combine into one and save to the clipboard. But, when I paste the text, it shows NULL on the places where the three imported core data entries are supposed to be. What have I done wrong? Here is some code:
NSString *namevalue;
NSString *versionvalue;
NSString *companyvalue;
namevalue = [self.device valueForKey:#"name"];
versionvalue = [self.device valueForKey:#"version"];
companyvalue = [self.device valueForKey:#"company"];
NSString *shareString = [NSString stringWithFormat:#"I have performed %# minutes of %# %#",namevalue, versionvalue, companyvalue];
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
[pasteboard setString:shareString];

NSTextfield storing in core data

i have a NSdatepicker and when i change the year, it changes another NSTextFile´s value to the number of years till now like this:
- ( IBAction)setExp:(id)sender {
NSDateFormatter *df = [NSDateFormatter new];
[df setDateFormat:#"yyyy"];
NSString *TodayString = [df stringFromDate:[NSDate date]];
NSString *TargetDateString = [df stringFromDate: startedPicker.dateValue ];
NSTimeInterval time = [[df dateFromString:TodayString] timeIntervalSinceDate:[df dateFromString:TargetDateString]];
int days = time /60 /60 / 24 /365;
NSString *intString = [NSString stringWithFormat:#"%d", days];
[expLbn setStringValue:intString];
}
but the new data from this action docent get stored in the core data for the NSTextField, if i type in a number in the same textfield the data gets stored.
i tryed http://www.red-sweater.com/blog/229/stay-responsive but can't make that work.

Problems converting a NSDate to a string

This extremely simple task was apparantly very hard...
NSDate *date;
date = [someMethod here];
//I've checked with debugger that the method returns an object of type NSDate.
//The description of date at this point is: "2012-02-02 19:42:00 +0000"
NSDateFormatter *dateFormat;
[dateFormat setDateFormat:#"dd-MM-yy hh:mm"];
NSString *dateString = [dateFormat stringFromDate:date];
dateString is just NIL
Does anyone know what's wrong?
Edit: What i really want to achieve is simply:
NSString *receivedDate = #"2012-02-02T20:42:00+01:00";
NSString *fixedDate = [do some magic]
//value of fixedDate is now: "02-02-12 20:42"
As Anna said you need to allocate an instance of NSDateFormatter because all you get is a NULL ptr, which just ignores the messages setDateFormat and stringFromDate, leaving you with NULL.
But also your format is for hours is incorrect. Refer to Date Formatter reference
This works for me:
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:##"dd-MM-yy HH:mm"];
NSString *dateString = [df stringFromDate:date];

ios4 - splitting a date and time from a string into two separate strings

I have a JSON feed coming into my app, one of the fields is a combined date & time string which I need to split into discrete date and time strings for display in a table cell. An example of input from the JSON is:
2012-01-18 14:18:00.
I'm getting a bit confused with the date formatter, and clearly I'm not doing it right - I've tried a number of tutorials but most just seem to show how to format a date.
I've tried something a little like this to get just the time:
NSDictionary *rowData = [self.raceData objectAtIndex:[indexPath row]];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setDateFormat:#"h:mma"];
NSDate *raceDate = [dateFormat dateFromString:[rowData valueForKey:#"race_time"]];
NSString *raceTime = [dateFormat stringFromDate:raceDate];
but on output raceTime is just null.
Any help appreciated.
maybe the format should be more like
[dateFormatter setDateFormat:#"yyyy-MM-dd 'at' HH:mm"];
have a look at http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html
might clear things up abit
Right, I have this working - it's probably a bit messy but here's what I did:
NSDictionary *rowData = [self.raceData objectAtIndex:[indexPath row]];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate* raceDate = nil;
NSError* dateError = nil;
NSRange dateRange = NSMakeRange(0, [[rowData valueForKey:#"race_time"] length]);
[dateFormat getObjectValue:&raceDate forString:[rowData valueForKey:#"race_time"] range:&dateRange error:&dateError];
[dateFormat setDateFormat:#"HH.mm"];
NSString *raceTime = [dateFormat stringFromDate:raceDate];
I can now output raceTime as a standalone time. I had to use getObjectValue:forString:range:error: to parse the original string to a date before changing the formatting and parsing it again.
As I'm using this in a table I suspect I'll need to use a static formatter so it doesn't slow everything down - if anyone can give a best practice on doing that I'd appreciate it.
If you are sure that the input string format wouldn't change – you might use something similar to:
NSString *date = nil;
NSString *time = nil;
NSDictionary *rowData = [self.raceData objectAtIndex:[indexPath row]];
NSString *raceTime = [rowData valueForKey:#"race_time"];
NSArray *dateParts = [raceTime componentsSeparatedByString:#" "];
if ([dateParts count] == 2) {
date = [dateParts objectAtIndex:0];
time = [dateParts objectAtIndex:1];
}

Resources