Cocoa Webkit bug? - cocoa

I have some code that gets the URL from an array and stores it as a string then uses that string to load up the URL.
NSString *first = [urls objectAtIndex:[sender clickedRow]];
NSLog(#"%#", first);
[[webview mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:first]]];
However, when I run this I get this error:
-[NSURL length]: unrecognized selector sent to instance 0x164dc0
The link in this case is http://www.digg.com
I know that the bug is in the line
NSString *first = [urls objectAtIndex:[sender clickedRow]];
because I tried setting the string's value directly to the URL instead of the array and it worked.

However, when I run this I get this error:
-[NSURL length]: unrecognized selector sent to instance 0x164dc0
First off, that's an exception, not an error.
When you get a message like this, read what it says:
-[NSURL
The object you sent this message to was an NSURL object.
length]:
The selector of the message was length.
Now, why would you send a length message to an NSURL object? You wouldn't, and you didn't do so yourself. Something else did.
But you would send a length message to a string object. So, you have an NSURL object, and you passed it somewhere that expected a string.
There's only one passage in the code you showed:
[NSURL URLWithString:first]
The exception tells you that first is already an NSURL; it is not a string. You do not need to create an NSURL from it, since it already is one, and trying to treat it as a string in any way will cause an exception.
You may be about to object to my claim on the grounds of this previous line:
NSString *first = [urls objectAtIndex:[sender clickedRow]];
Your objection would be that the declaration clearly says that first is a pointer to an NSString, so I must be wrong.
But that is not so. You declared first as a pointer to an NSString. That is to say, you told the compiler that the variable first would hold a pointer to an NSString.
But then you put a pointer to an NSURL into the variable.
In many cases, the compiler would warn you that you have lied to it, but it didn't in this case because the object came through objectAtIndex:, which returns id; thus, the compiler doesn't know what type of object you're putting into the variable. The compiler, assuming that you told the truth and really are putting an NSString here, does not warn for this initialization.
But you're not. The object is an NSURL, as you found out at run time.
The fix is two-fold:
Restore truth to the declaration, by declaring the variable as NSURL *, not NSString *.
Don't attempt to create an NSURL here, because you already have one.

Related

Filter NSArray with NSDictionary (nested level of object) using NSPredicate ANY and IN?

I have one main array named originalListArray with multiple objects.Here is the example of object.
Example.json
I want to find that number of objects from the originalListArray, who have amenity_id matched with my filter data.
I have do this, create one predicate usign ANY not self becuase NSArray with NSDictionary and in that Dictionary object may have NSArray data.
This is working fine.
NSPredicate *predicate=[NSPredicate predicateWithFormat:#"ANY amenities.amenity_id =='1')"];
This is not working.
NSPredicate *predicate=[NSPredicate predicateWithFormat:#"ANY amenities.amenity_id in ('1','2','3')"];
So Single value can be filter easily but usign IN oparation its crash and error is like this.
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "ANY amenities.amenity_id in ('1','2','3')"'
Thanks in advanced, if any one can help me in this.
I think it's better to pass the array as a parameter with %# placeholder.
[NSPredicate predicateWithFormat:#"ANY amenities.amenity_id IN %#", #[#"1",#"2",#"3"]]
That way, you can create manually your array, and if you need to change it, it's easier.
If you still want it to do it manually changing the "string format":
[NSPredicate predicateWithFormat:#"ANY amenities.amenity_id IN {'1', '2', '3'}"]

NSData dataWithContentsOfURL returning nil specifically in iOS 8

I am tring to get the NSData from a local file using [NSData dataWithContentsOfURL:] but am getting nil even when the fileSize returned by [NSFileManager defaultManager] is a positive integer.
Surprisingly, I got this issue when the base sdk was increased from iOS 7 to iOS 8.
I am not sure whether it is an iOS bug or what as it worked in iOS 7 base SDK and not with iOS 8, but here is the solution I found after wasting a couple of hours debugging the code :-(
Use [NSData dataWithContentsOfFile:(NSString *)] instead of dataWithContentsOfURL
Are you sure you pass the Bundle path as well ?
Try with this
NSString* path = [[NSBundle mainBundle].bundlePath stringByAppendingPathComponent:yourPath];
NSData* data = [NSData dataWithContentsOfURL:path];
NSData and URLs: There be dragons
+[NSData dataWithContentsOfURL:] can return nil for any number of reasons. If anything goes wrong when attempting to use that URL this method will return nil. For example the file may not exist, the network connection may time out, or the URL may even be malformed. nil is returned and the application has no idea why.
+ [NSData dataWithContentsOfURL:options:error:] on the other hand, will tell the caller what went wrong. When nil is returned the error argument will be populated with an object that describes the problem that occured. Using this method would directly answer the question of "why".
Both of these are synchronous methods and their use for working with files, network resources, and especially files served from a network resource is discouraged. These methods will block the caller and are not really intended for these kinds of uses. It's better to use an input stream or NSURLSession instead.
you can reference enter link description here
My case. it had a space in the URL
remove space

Potential leak of an object - NSData

When I Analyze a class I'm working on this line of code:
myObject.myImageData = [NSData dataWithContentsOfURL:[[NSURL alloc] initWithString:myObject.thumbnailUrlString]];
has a warning Potential leak of an object.
Any idea why and how you fix it?
==== Note
If I try and separate this line out I get additional errors, e.g.
NSData *myImageData = [NSData dataWithContentsOfURL:[[NSURL alloc] initWithString:myObject.thumbnailUrlString]]; // 1. Method returns an Objective-C object with a +0 retain count
myObject.myImageData = myImageData;
[myImageData release]; // 2. Incorrect decrement of the reference count of an object that is not owned at this point by the caller
You don't have ARC turned on. You almost certainly should turn on ARC so that the system will handle all of this for you.
That said, this is a basic manual memory management error, and the analyzer is telling you so.
myObject.myImageData = [NSData dataWithContentsOfURL:[[NSURL alloc] initWithString:myObject.thumbnailUrlString]];
This leaks the NSURL you created with +alloc. You need to call release on it at some point, but you no longer have a pointer to it. The usual way to fix this is with an autoreleased NSURL:
myObject.myImageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:myObject.thumbnailUrlString]];
But the much better way to fix it is to turn on ARC.
Your attempt at fixing it releases the wrong object. You don't own myImageData. You didn't create it with alloc, new, or copy, and you didn't call retain on it. The analyzer is warning you that you're incorrectly releasing it.

-[NSKeyedUnarchiver initForReadingWithData:]: data is empty; did you forget to send -finishEncoding to the NSKeyedArchiver?

I get this warning only on my first item on a table view once i go into the "drill down" view on a core data app.
anyone else got this warning?
-[NSKeyedUnarchiver initForReadingWithData:]: data is empty; did you forget to send -finishEncoding to the NSKeyedArchiver?
thanks
Xcode 7.2.1, iOS 9.2.1, ARC enabled
Check to see that the NSData object you are using to store data does not get released before it is accessed. You must check this at the place where the data is accessed, not in your view controller or else where.
-[NSKeyedUnarchiver initForReadingWithData:]: data is empty; did you forget to send -finishEncoding to the NSKeyedArchiver?
This warning is raised when the NSData object is empty. The sure way to check if it is or not, is to use [yourDataObject length] and make sure it is not zero.
Hope this helps! Cheers.
once I had met this problem, It's cause by...
NSString *str = #"ss";
NSString *temp = [str substringToIndex:4];
In iOS8.
now xCode8.1 will tell you
* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSCFConstantString substringToIndex:]: Index 4 out of bounds; string length 2'
It seems that you are trying to read from an empty data object.
Maybe you initialize your data as [NSData data] before or your saved data is empty.

[__NSDate length]: unrecognized selector sent to instance

I have a datePicker and I trying send the value but don't working it.
The Error:
-[__NSDate length]: unrecognized selector sent to instance 0x8ee2330
2014-06-30 13:49:42.602 Golf Tipp[2374:60b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDate length]: unrecognized selector sent to instance 0x8ee2330'
**My Code:**
Confirmacion *cuartoView = (Confirmacion *)[segue destinationViewController];
NSDate *date = [self.fecha_hora date];
cuartoView.fecha = date;
You have an object of type NSDate. And somewhere you have code that believes the object is an NSString or maybe NSData (not NSDate), and sends a length message to it.
The usual way to find the problem is either: Stare at your code long and hard. Or: Set an exception breakpoint in Xcode and find where the problem happens, and deduce from that what you did wrong. If you don't know how to set an exception breakpoint in Xcode, feel free to use Google.

Resources