How do I write ALAsset in a NSData - iphone

I'm trying to write ALAsset in a NSData and somehow came up with the following code which does not work. What I would like to know is, whether the code is similar to this? Or is there another way of doing it?
NSData *data = [[NSData alloc] initWithContentsOfFile:#"assets-library://asset/asset.MOV? id=100000009&ext=MOV" ];

The defaultRepresentation of an ALAsset has a getBytes:fromOffset:length:error: method. Use that.

There's a space between your "?" and "id=.." is this the problem?
The best way to debug this is to first try opening that file you name (or creating an NSURL and using initWithContentsOfURL as suggested by Max) and then running the debugger, checking whether that succeeds. Right now you're debugging a few things simultaneously, break out that compound statement and figure out what could be going wrong

Related

NSURL subclass, with file URL, doesn't work in QuickLook, for non-ASCII filenames?

There's a lot of pieces to this, but from what I can tell, they're all necessary to reproduce the problem.
I made a trivial NSURL subclass, like so:
class URL2: NSURL { }
I made a file URL with it:
let f = URL2(fileURLWithPath: "/Users/me/Downloads/ついて.pdf")
Then I tried returning it from previewPanel(panel:previewItemAtIndex index:).
Result: I get a generic file icon in the QLPreviewPanel (but it has the correct filename).
Curiously, if I do any of these differently, it works:
If I use a plain NSURL(fileURLWithPath: "/Users/me/Downloads/ついて.pdf"), it displays the file contents correctly.
If I use an ASCII-only filename, it displays the file contents correctly.
If I do something else with the URL2 (like some NSFileManager operation), it locates the file just fine.
What could I be doing wrong that causes QuickLook to be unhappy with my NSURL subclass in this case?
Subclassing NSURL (or NSURLRequest) is often a path to madness, thanks in no small part (I think) to heavy use of NSXPC in various parts of the OS.
I would suggest using a category with associated objects instead. This should avoid the edge case you're hitting, while still allowing you to add custom methods and properties to the NSURL objects.

Incompatible Objective C types

I am trying to store the location of my Application's Document Directory in a NSString.
NSString *documentDirectory = [self applicationDocumentsDirectory];
The code compiles without error, but I get the warning that I am trying to initialize a NSString with a NSURL. When I run the app, the app crashes. I tried casting, but it gave me an error. The book I got it off(Head First iPhone Development) says the code should work. The code is in my appDelegate.m file. Any Help will be greatly appreciated.
Regards
It is returning an NSURL as Dave DeLong said. However the easiest way to get round this and convert it to an NSString without changing the method, is setting the string to the NSURL's absoluteString.
NSString *documentDirectory = [[self applicationDocumentsDirectory] absoluteString];
Your -applicationDocumentsDirectory method is returning an NSURL. You want it to return an NSString.
The methods return a NSURL. You must convert the url in NSString.
It's very easy. Use a method in NSURL class.

iPhone memory leak issue in ZipArchive lib

I'm gratefully using the ZipArchive library but it seems there is a memory leak. I don't know how to fix this - it's written in C and I'm unfamiliar with it. My question is - is it possible to autorelease the line s=(unz_s*)ALLOC(sizeof(unz_s)); like you would in Objective-C in this scenario?
extern unzFile ZEXPORT unzOpen2 (path, pzlib_filefunc_def)
const char *path;
zlib_filefunc_def* pzlib_filefunc_def;
{
// ...
s=(unz_s*)ALLOC(sizeof(unz_s));
*s=us;
unzGoToFirstFile((unzFile)s);
return (unzFile)s;
}
Here is a screen grab of the location of the leak for clarity:
http://ziparchive.googlecode.com/issues/attachment?aid=-5463964192517894688&name=Screen+shot+2010-08-20+at+8.12.58+PM.png&token=8c66aa58a4826b99ba157903fbae83bb&inline=1
Can anybody could shed some light on how I might fix this? Thanks.
i have faced the same problem and Solved by reading some of the blogs . it seems to simple
close your ZipArchive Object before relesing Objectits look like [ZipObj UnzipCloseFile];
No, you cannot autorelease it. Autoreleasing is only available for Objective-C objects. So you have 2 options:
Free the memory yourself using free().
Wrap the s thing into an NSData using dataWithBytesNoCopy:length:, which will take ownership of the allocated data and free it when the NSData object is deallocated.
Option 2 would look something like this:
unz_s * s = unzOpen2(...);
NSData * boxedS = [NSData dataWithBytesNoCopy:s length:sizeof(unz_s)];
Then when boxedS gets destroyed, it will free s as well.

Correctly debugging IPhone Apps on XCode

The line below has an error, can you see it with a naked eye?
NSString *title = [sender titleForState:UIControlStateNormal];
NSString *newText = [[NSString alloc] initWithFormat:"%# button pressed.", title];
When the Iphone simulator executes this line it crashes without any other information. I must be missing something about debugging. The console showed me nothing.
I was able to figure out the error, my question is not about that. My question is why I get a hard crash with no help from XCode. Without any clue it took me precious 5 minutes before I could realize my typo.
Any suggestions? Or when programming for IPhone I just need to be very careful with typos?
EDIT: I guess some people did not see it immediately like me. The error is the lack of '#' for the string constant. So now my question is why XCode/Simulator did not show me any kind of error message, just crashed without any clues. Am I missing something about debugging?
Objective-C does not strongly verify that the arguments you pass to messages are of the right type during compilation nor at runtime. It should gives you a warning though. Here you pass a c string instead of a NSString. Because NSString are objects (thus referenced using pointer), your method uses it as a pointer while you feed it with a simple string. You then probably try to access unaccessible memory blocks...
I think you miss a # before the "%# button pressed".
The correct one should be:
NSString *newText = [[NSString alloc] initWithFormat:#"%# button pressed.", title];
All the constant NSString should be #"SOMETHING HERE";
Check you compilation warnings. That's all you need. On the case you are showing, you will get a proper warning that will alert you that bad things might happen at that line.
I get the following Error when compiling your code:
error: cannot convert 'const char*' to 'NSString*' in argument passing
Not sure what you need to do to get it to show you that, I'm working in Obj-C++.
Try adding "-Wall" to your "OtherWarningFlags" under your target's build settings.

iPhone: Missing method? NSDictionary dictionaryWithData:

A web service provides data in the form of a plist. After the download, I have all of the data in an NSData object, which I want to convert to an NSDictionary. Right now the only way I know of to do that without parsing it by hand (yuck) is this:
static NSString *fileName = #"tempFile";
[data writeToFile: fileName atomically: NO];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile: fileName];
I'm surprised NSDictionary doesn't have a dictionaryWithData: method. I wouldn't even mind converting the NSData to an NSString first, if dictionaryWithString: existed. Clearly the SDK is doing something similar under the hood, but it isn't exposed to developers.
Can anyone suggest a better way to do this? One of the files I need to convert can potentially be pretty big, and doing the write-to-file+read-from-file operations could be a bit slow.
Use one of the following:
+[NSPropertyListSerialization propertyListFromData:mutabilityOption:format:errorDescription:]
+[NSPropertyListSerialization propertyListWithData:options:format:error:]
The former is available in iOS 2.0+ but has been deprecated in 4.0.