I have converted CGPoint To NSValue and put all the resulting values in q NSArray but when I use [NSKeyedArchiver archiveRootObject:ArrayName toFile:PointArrayFile]; it gives me error.
So what's the problem?
You don't say what problem you're seeing but in general you're probably better off using NSStringFromCGPoint and CFPointFromString to store and retrieve the points.
Related
Thanks To all that helped problem solved. :)
For some reason this wont work for me please help
array = [[NSMutableArray alloc] init];
inputted = [input.text doubleValue];
[array addObject:[NSNumber numberWithDouble:inputted]];
NSLog(#"%i",array.count);
where array is a NSMutableArray, inputted is a double and input is a text field
All that happens is that one saves but deletes the last one entered. how do i make it so that it saves everything entered?
You're always re-creating and re-initializing the "array" mutable array each time you go through your function so it's no wonder you are getting a result of "1" (one object in the array).
If you initialize your array once and only once, and move it out and away from the rest of that code (i.e. into a different function or whatever), then you will add additional objects to your mutable array and you'll see the count increment each time you add an object to your mutable array.
Makes sense?
I have two NSData objects
NSData *toScan = /* initialized somehow with "Hello, this world." */;
NSData *toMatch = /* initialized somehow with "this" */;
What is the best way to know the toMatch bytes are the subset of toScan bytes?
I use C-functions for this purposes at this point: something like this
strstr([[toScan identifier] bytes], [[toMatch identifier] bytes]);
// returned bytes are null-terminated
but I think there is not the best way to use C-functions in object-oriented environment...
As of Snow Leopard and iOS 4.0, NSData has -rangeOfData:options:range: which should do what you want.
It's also pretty darn fast.
In a different question, I wrote an answer containing an NSData category with a rangeOfData: method:
Elegant Algorithm for Parsing Data Stream Into Record
That'll do what you're looking for.
So I have an NSDictionary that contains several objects with properties (key:values), but the issue is that I need them in a specific order.
Stored as an NSArray is a list of objects that I would like to use as keys for the NSDictionary.
I can output the NSArray and NSDictionary to the Console, however when I to access each of the values keyed to "filename" with:
for(NSUInteger i=0; i<[images count]; i++)
NSLog(#"%#", [[dictionary objectForKey:[images objectAtIndex:i]] objectForKey:#"filename"]);
I get all nulls.
Anyone have any idea? I'm assuming it's because my [images objectAtIndex:i] returns something other than a string, however I've tried slappin in a (NSString *) and I still get nulls.
If the things in the dictionary aren't dictionaries, shouldn't that second objectForKey be valueForKey?
NSLog(#"%#", [[dictionary objectForKey:[images objectAtIndex:i]] objectForKey:#"filename"]);
I get all nulls.
Getting the key from the array is working fine. Either dictionary does not have an object for the key you retrieved, or the dictionary you retrieved does not have an object for the key #"filename".
I'm assuming it's because my [images objectAtIndex:i] returns something other than a string, however I've tried slappin in a (NSString *) and I still get nulls.
First, NSStrings are not the only valid keys. Any copyable object is usable as a dictionary key.
More importantly, remember that you never work directly with object structures, but with pointers to objects. objectAtIndex: returns a pointer to an object; when you cast its return value, you're telling the compiler that objectAtIndex: will return a pointer to an NSString object. Casting a pointer does not change the pointer itself; it only tells the compiler what to expect at the other end of that pointer. If objectAtIndex: is returning pointers to, say, Foo objects, casting them to pointers to NSString objects will not make them point to NSString objects; they will still point to Foo objects, so looking those objects up in a dictionary whose keys are strings will still fail.
The other thing to consider is: Why don't you know what is in this array? You created the array. You populated the array. Surely, then, you should know what's in it. objectAtIndex: does not return anything but (a pointer to) an object that is in the array.
If you think objectAtIndex: is returning objects that are not valid keys, then you should examine the array using either NSLog or the debugger. Check what the array contains and what those objects' classes are.
More probably, the array does contain objects that would be valid as keys, but are not keys in the dictionary, or it does contain objects that are keys in the dictionary, but the dictionaries that are the objects for those keys do not contain the #"filename" key. Examine both the array and the outer dictionary.
One other caveat, which may be what's tripping you up: If you're keeping mutable strings in the array, and those are the keys you initially inserted dictionaries into the outer dictionary under, and you mutate these strings, that makes them not match the keys in the outer dictionary. Remember that dictionaries copy their keys (this is why keys need to be copyable); you mutate the string you have in the array, but the dictionary made a copy previously, and that copy does not change. (Such is the point of making a copy: Whoever makes the copy—in this case, the dictionary—wants to keep the object as it is, without receiving any mutations that occur later on.)
So, if you want to revise any of the strings, you'll have to replace them in the array and store the outer dictionary's object for the old key under the new key. You'll also have to take steps to prevent duplicate keys; the dictionary only holds one object per key, while the array can contain the same object any number of times.
What is the best way to count the number of entries in a property list?
I currently build a dictionary from the plist entries (*) and then use the dictionary's count:
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:myPlistPath];
NSDictionary *myPlistDict = (NSDictionary *) [NSPropertyListSerialization
propertyListFromData:plistXML
mutabilityOption:NSPropertyListMutableContainersAndLeaves
format:&format
errorDescription:&errorDesc];
NSLog(#"There are %d entries in the plist.", [myPlistDict count]);
This strikes me as unnecessarily "heavy", but I was not able to find a more efficient solution. Any ideas?
(*) targeting 10.5 and therefore using the deprecated +propertyListFromData:… class method.
Well... if you're converting to XML anyway, you could use NSXMLNode's childCount method. The documentation does suggest that it's more efficient than calling [children count], but the creation of the NSXMLNode might make this just as bad (or even worse than) the NSDictionary method.
Have you profiled? Are you working with particularly large plists? Are you requesting this count often? I say: use NSDictionary, cache the value if you request it often, and move on unless this is unacceptably slow. (Yeah, it looks ugly right now, but there are bigger things to worry about.)
A real beginners question.
I have a NSView subclass in which I create a NSMutableArray containing NSValues. When I want to write the array to a file using writetofile:atomatically: the file is created but it contains none of the NSValues that the mutable array does contain.
Does anyone know how I successfully can write this mutable array to a file?
Thanks
NSValues can't be saved in a plist (which is what writeToFile:atomically: does). Take a look here for the values you can save. (NSNumber is a kind of NSValue you can save, but other NSValues will fail.)
If you want to save your array with NSValues, you can use archiving instead of writeToFile:atomically:. NSArray and NSValue both support archiving, so you just convert the array to an archive and save that data to a file. (It will include the NSValues as well.) The code looks something like this:
[NSKeyedArchiver archiveRootObject:myArray toFile:#"myPath"];
To load it, just use:
NSArray *myArray = [NSKeyedUnarchiver unarchiveObjectWithFile:#"myPath"];