Copy Multiple files to clipboard in objective c - objective-c

I'm trying to copy multiple files to the clipboard and I'm not sure what I'm missing. I wrote this little program just to test putting one file on the clipboard, but after it runs, there's nothing on the clipboard. I can't see what I'm missing. Here's the code I'm running:
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
NSInteger changeCount = [pasteboard clearContents];
NSMutableArray *filesToCopy;
NSString* pathToFile = #"/Users/user/Downloads/file1.jpg";
NSURL* fileURL = [[NSURL alloc] initFileURLWithPath:pathToFile];
[filesToCopy addObject:fileURL];
BOOL OK = [pasteboard writeObjects:filesToCopy];

Put a breakpoint on the line:
[filesToCopy addObject:fileURL];
When it is reached examine the variables involved, see the problem?
To convince yourself you are right step over the statement and then examine the variables again.
HTH

Actually, I feel silly now. The problem was that the array was never actually initialized. I'm surprised an exception wasn't thrown because addObject was done on a nil object. The line
NSMutableArray *filesToCopy;
should be
NSMutableArray *filesToCopy = [[NSMutableArray alloc] init];

Related

Can't release componentsSeparetedByString array

I have a simple method run in background thread which open txt file and split it on lines. After that I'm trying to release memory, but something goes wrong. I'm using ARC. Here's code:
#autoreleasepool {
NSString* file = [NSString stringWithContentsOfFile:resourcePath encoding:NSWindowsCP1251StringEncoding error:&error];
NSArray* test = [file componentsSeparatedByString:#"\n"];
test = nil;
}
String released fine, but array still in memory. What I've missed?
UPD: Hm... Just tried to duplicate array few times, and after end of the method array really deallocates. But there is memory leak if I create this array. Where it could be?
// test = nil;
Dismiss it, and ARC will work fine.

Reading strings from txt files into NSArrays in iOS

After much reading it seems that, really, the only way to read a number of lines from a text file into an NSArray is with this:
NSString *myfilePath = [[NSBundle mainBundle] pathForResource:#"poem" ofType:#"txt"];
NSString *linesFromFile = [[NSString alloc] initWithContentsOfFile:myfilePath encoding:NSUTF8StringEncoding error:nil];
myArrayOfLines = [NSArray alloc];
myArrayOfLines = [linesFromFile componentsSeparatedByString:#"\n"];
NSArrays have a method for initWithContentsOfFile but I have not seen any examples of how to use this. I have read some posts that state that the file must be a plist and not a generic txt file.
Is this really the case? Is there a way to read lines (terminated with \n) directly into an NSArray?
You have it right, except the line myArrayOfLines = [NSArray alloc]; which is useless.
Don't bother with plist if you already have a good txt file.
But for curiosity, here is a link which explains how it works with plist files : link
Also, if you don't use ARC, you'll have some leaks, but that's another question, and we don't have the whole code, so I might be wrong.

NSString WriteToFile not stay permanent?

I am trying to save text stored in an NSString variable in a text file that is stored with the main bundle of my project.
So far I have had no success and tried a lot of different methods.
Why doesn't this stay permanent?
NSString *pathToFile = [[NSString alloc]init];
pathToFile = [[NSBundle mainBundle] pathForResource:#"ListOfSavedImages" ofType:#"txt"];
NSLog(#"%#",pathToFile);
NSString *stringToWriteToFile = [[NSString alloc]init];
stringToWriteToFile=#"Adam";
NSLog(#"%#",stringToWriteToFile);
[stringToWriteToFile writeToFile:pathToFile atomically:YES encoding:NSUTF8StringEncoding error:NULL];
NSLog(#"called!");
NSString *contentsOfFile1 = [NSString stringWithContentsOfFile:pathToFile encoding:NSUTF8StringEncoding error:NULL];
NSLog(#"%#",contentsOfFile1);
The actual file doesn't change although the NSLog at the end of this code segment outputs "Adam" but I am also nslogging the contents of the file when the view loads and it always reverts back to the original text(it never actually changes). What am I doing wrong?
I am using Xcode 4.3, ARC, and storyboards.
As you are instantiating your variables locally, they will leak away when you hit the end of the block }.
Try using IVars declared as properties of the particular view controller, synthesized in the .m file.
Look at the C139p at Stanford Course on ITunes, preferably the earlier series given before ARC as this fully explains the concept of data persistence.

Reused code to read from file causes EXC_BAD_ACCESS only in second class

I use this code in two different files to fill the categories array from a string of text from a text file, in which entries are separated by double pipes.
In the first file, my appViewController class, everything is fine. In the second, popoverViewController, the program bombs with EXC_BAD_ACCESS on the arrayWithArray: line. Declarations for categories, tempArray, diskfile, and textFromFile are the same in both interface files.
NSLog tracers and breakpoints confirmed values of variables are the same down to that last fatal line. The popover contains a picker, so picker delegate and datasource protocols are in place. That's the only difference. Can anyone explain what might be going on?
categories=[[NSMutableArray alloc] init];
tempArray = [[NSMutableArray alloc] init] ;
NSMutableString *textFromFile=[[NSString alloc] init];
NSString *filePath = [[NSBundle mainBundle] pathForResource: #"Categories" ofType: #"txt"];
if (filePath) {
textFromFile = [NSString stringWithContentsOfFile:filePath];
categories=[NSMutableArray arrayWithArray:[textFromFile componentsSeparatedByString: #"||"]];
}
Set NSZombieEnabled, MallocStackLogging, and guard malloc in the debugger. Then, when your App crashes, type this in the gdb console:
(gdb) info malloc-history 0x543216
Replace 0x543216 with the address of the object that caused the crash, and you will get a much more useful stack trace and it should help you pinpoint the exact line in your code that is causing the problem.
See this article for more detailed instructions.

Converting File Path From NSString To NSURL

I'm working through Cocoa smoothly, but this problem seems so basic it cancels out all the cool stuff I learned. :/
I have a generated file path, and it needs to be in NSURL format. From research, this is the code I wrote:
NSLog(#"Old path = %#", pathToFile);
NSURL *xmlURL = [[[NSURL alloc] init] fileURLWithPath:pathToFile];
NSLog(#"New path = %#", [xmlURL absoluteString]);
And the output:
2010-01-27 15:39:22.105 MusicLibraryStats[28574:a0f] Old path = file://localhost/Users/[username]/Music/iTunes/iTunes%20Music%20Library.xml
2010-01-27 15:39:22.105 MusicLibraryStats[28574:a0f] New path = (null)
First off, the alloc-init shouldn't even be necessary; other people seem to get away with it. In this case, if I don't alloc-init, I get an 'unrecognized selector' error on that line. Of course, now I'm just getting plain old (null).
Where did I goof?
Thanks!
The [[NSURL alloc] init] is not just unnecessary, it's invalid. fileURLWithPath: is a class method, which means you can only call it on the class object (that is, NSURL itself). It does not produce a compile error because -(NSURL *)init returns an object of type id, and does not result in a runtime error because -(NSURL *)init actually returns nil, and messages sent to nil will just cascade another nil as their return value.
This code should work:
NSString* pathToFile = #"/this/is/a/path";
NSURL* url = [NSURL fileURLWithPath:pathToFile];
I found your problem.
-[NSOpenPanel URLs] returns an array of NSURL objects, which you treat as NSString objects. That's not right. You should use the following:
NSURL* url = [[oPanel URLs] objectAtIndex:0];
The debugger could've show you that if you looked at the pathToFile variable. Make sure to check it next time. :) Hovering a variable with your mouse should get you its type.
However, remember that there are situations where you will legitimately encounter another type than the one you expected. For instance, the private NSPathStore2 class is part of the NSString cluster, and you can do everything NSString supports on NSPathStore2 objects. (If this happens and you're not too sure, check the documentation to see if the type you expect is a cluster type. That's how they're called in the documentation.)