0

I have an object that defines a property as an NSURL:

@interface fcpElement : NSObject
@property (copy) NSString* elementName;
@property (copy) NSURL* elementPath;
@property (copy) NSURL* elementParent;
@property () BOOL elementIsHidden;
@property (copy) NSString* elementType;

-(id)initWithName : (NSString*) elementName path: (NSURL*) elementPath parent: (NSURL*) elementParent hiddenValue: (BOOL) elementIsHidden type: (NSString*) elementType;

@end

In my app controller I create an NSMutableArray and populate it with my objects using my init… method. I then have a button which calls a method on the app controller which creates a new NSURL by calling the instance variable from an object in the array, as follows:

for(currentElement in _finalCutData) {
    NSURL *currentElementPath = [currentElement elementPath];

Eventually I am wanting to do a comparison to see if this new URL is equal to another, but I always get errors that stop my program if I do anything like the following:

NSURL *currentElementPathAbsolute = [currentElementPath absoluteURL];

with the error: -[__NSCFString absoluteURL]: unrecognized selector sent to instance

If I add a breakpoint it says that currentElementPath is an invalid pointer. But if I NSLog [currentElement elementPath] I get the URL contained within.

How do I get the URL from my instance variable such that I can use it? Am I using the wrong parameter types in my property declarations? Or is it something else?

4
  • Apparently currentElementPath is an NSString, not an NSURL. What does the elementPath method do? I don't see it in the docs - is it something you wrote? Commented Jul 20, 2013 at 17:56
  • “If I add a breakpoint it says that currentElementPath is an invalid pointer.” Where did you set the breakpoint? If you set it on the line that declares currentElementPath, then its initialization hasn't happened yet, so you'd need to step once (i.e., “do this line”) before the variable will have a value for you to print. The alternative is to set the breakpoint any subsequent line in the method; I would set it on the very next line. Commented Jul 20, 2013 at 19:47
  • The generic answer would be to run your app under Instruments with its Zombies template and see what killed off the URL before you tried to send it an absoluteURL message. More specifically, I think it's something in between those two points, namely currentElementPath's declaration and the absoluteURL message, that you didn't show. Please edit your question to include all of the code between the declaration of currentElementPath and the use of it in the absoluteURL message. Commented Jul 20, 2013 at 19:51
  • I have updated my code which had some how omitted the rest of the property declarations in my object, in case that helps. @PeterHosey the absoluteURL declaration is the next line of code after my retrieval of the instance variable, it was all in one line of code inital but i split it into 2 to try and work out where the problem was. Commented Jul 20, 2013 at 23:06

2 Answers 2

1

Your problem is most likely that this call:

[currentElement elementPath];

is returning a string, not a URL. How is -elementPath implemented? Are you seeing any compiler warnings?

I assume -elementPath is implemented to be a simple getter method (perhaps as an @property). In which case, the fault lies in whatever code is storing that value in the first place.

Are you using ARC or manual memory management? If the latter, there's also a chance you've got a zombie here. You're not retaining the URL, and so it's being deallocated, and later replaced with a string.

Sign up to request clarification or add additional context in comments.

3 Comments

elementPath is as you say a simple getter method as an @property (as listed in the fcpElement object in my code above. The objects in my array are getting their values from the following code:NSURL *filePath; [currentFile getResourceValue:&filePath forKey:NSURLPathKey error:NULL]; current file being the current object in an enumerator. I am using ARC
@NicholasWarren: The value of NSURLPathKey is a path (which is a string), not a URL. If you want a URL, you already have one—that's what currentFile is. If you want a path (which you generally should not), you need to expect a string and handle it as such.
@PeterHosey Thanks very much - that was the solution. Now works as I expected.
0

I'm surprised it seems I can help on this forum, since I'm a bit of a beginner, but the NSURL class has initialization methods that you should use, like initFileURLWithPath: You probably shouldn't override the NSURL initialization methods.

3 Comments

Where is the questioner “overrid[ing] the NSURL initialization methods”?
I'm not overiding the initialisation methods, just can't seem to get hold of the NSURL that I have as a property inside my object without it causiing errors, unless i'm nsloging it.
I apologize, I had thought your class was NSURL. The answer is essentially the same, that you are not properly initializing your NSURL object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.