Question

I'm a total newby with Objective-C, so bear with me. I'm not looking for a code handout, but some guidance on where to look for answers would be appreciated.

I'm a motion picture editor and I prototyped a small utility in Python which reads an Edit Decision List (which is a text tile with timecodes and various other pieces of information pertinent to video editing) and parses the file one line at a time and extracts certain information to create a subtitle list.

Once I got it working in Python, I made the brash decision to migrate the code to Objective-C so I could distribute my utility to other editors as a binary executable.

So much for the backstory, now for the question...

Last night I managed to build the GUI, which is a simple button to initiate a file requester (to load the text file) a button to convert the file, and a button to save the newly converted file. There's also a text view object to display the converted file. I managed to attach all widgets to the new class and I can test for each button being clicked, and I can manually write text to the text view widget. So far, all's good.

The problem I'm having is figuring out how to pass information from one method to another. In particular, if I open the file using the openFile method, then how do I pass that information to the convertFile method? When I try, I get a message telling me that my openFile is undefined. I assume it's all about data encapsulation. But it's frustrating as heck, and I'm such a newbie, I'm not even sure what I should be looking at for answers. It was very easy in Python: any method in a given class seemed to allow for seamless transfer to any other method without problems.

To recap, I do have the file requester opening, and I'm able to read the file. I can even parse it line by line. But it's in it's own method, and I cannot for the life of me figure out how to pass that information later to the "convert" method and after that how to pass the converted file to the "save" method.

Guidance MUCH appreciated. Thanks in advance.

Chris Conlee

Was it helpful?

Solution

ObjC is not all that different from Python in this respect. In both languages, data can be associated with an object, local to a function/method, or global. You can pass data directly from one method to another as arguments, or indirectly by storing it somewhere that both methods have access to (for example, an instance variable).

For the former, you would do something like this (hypothetical implementation filled in, since I don't know what your methods do):

@implementation MyClass //in the .m
- (void) openFile:(NSString *)pathToFile {
    NSData *fileData = [NSData dataWithContentsOfFile:pathToFile];
    [self convertFile:fileData];
}

- (void) convertFile:(NSData *)fileData {
    //whatever you wanted to do to fileData
}
@end

For the latter, you'd need to declare storage on the object for the thing you want to store, as well as how that storage should be accessed. Modern ObjC combines these tasks with the @property syntax:

@interface MyClass : NSObject //in the .h
@property (readwrite, strong) NSData *fileData;
@end

@implementation MyClass //in the .m
- (void) openFile:(NSString *)pathToFile {
    self.fileData = [NSData dataWithContentsOfFile:pathToFile];
}

- (void) convertFile {
    //whatever you wanted to do to self.fileData
}
@end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top