I found this in my travels:
http://www.opensource.apple.com/source/IOKitUser/IOKitUser-502/ps.subproj/IOPSKeys.h
How would I get the values (as a number) of:
#define kIOPSMaxCapacityKey "Max Capacity"
and
#define kIOPSDesignCapacityKey "DesignCapacity"
I want to be able to use them throughout my app. What do I need to add to my project and how do extract the numbers?
Many thanks,
Stuart
Once you know where the actual dictionary is that it's storing these values, you can access the value from the dictionary using the following call:
CFDictionaryGetValue (
CFDictionaryRef theDict,
const void *key
);
Further examination of the directory http://www.opensource.apple.com/source/IOKitUser/IOKitUser-502/ps.subproj/ would probably allow you to figure out which objects/methods to call to get the dictionary, and the documentation within the class states what it points to (in your two previously mentioned keys, they point to CFNumbers, so in that case CFDictionaryGetValue will return a CFNumber.
Related
I am trying to write code for generating globally unique name for images that are to be uploaded by users from iOS app to server. The names should be randomly generated and should be unique so that the images are not overwritten/replaced.
Here's my code for generating random and unique strings:
+ (NSString *)generateRandNameWithLength:(int)len
{
NSString *letters = [NSString stringWithFormat:#"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ%#0123456789", [HJUtilities generateUniqueApId]];
NSMutableString *randomString = [NSMutableString stringWithCapacity: len];
for (int i=0; i<len; i++) {
[randomString appendFormat: #"%C", [letters characterAtIndex: arc4random() % [letters length]]];
}
return randomString;
}
Where:
+ (NSString *)generateUniqueApId
{
NSString *appId = (__bridge NSString *) CFUUIDCreateString (NULL, CFUUIDCreate(NULL));
return appId;
}
returns a UUID.
Now I'm not sure whether this is the correct code for generating globally unique strings. I don't know how to verify this to be certain that no user will overwrite another user's image.
Note: I'm using Amazon Web services for storage. And one common bucket will be used for all the images of all users. So, its required that images names should be unique.
There is no need for the code you have. All you need is the CFUUIDCreateString function. This will be unique across all users on all devices.
From the docs for CFUUID:
UUIDs (Universally Unique Identifiers), also known as GUIDs (Globally Unique Identifiers) or IIDs (Interface Identifiers), are 128-bit values guaranteed to be unique. A UUID is made unique over both space and time by combining a value unique to the computer on which it was generated—usually the Ethernet hardware address—and a value representing the number of 100-nanosecond intervals since October 15, 1582 at 00:00:00.
The code you have now is definitely not guaranteed to be unique.
There is a new class added in iOS 6.0
#interface NSUUID : NSObject <NSCopying, NSSecureCoding>
To simplify the memory management you for sure can use it
You're overcomplicating it. Just get a UUID using CFUUIDCreateString and use that string. Adding extra layers of randomness isn't going to help. In fact, it's probably going to make things worse by increasing the chance of a name collision.
The only argument against using a UUID directly is that it may be possible to identify the source of the upload, since the UUID is (or can be) generated using device's MAC address, which is specific to the hardware. You won't be able to identify a user with nothing but a UUID, but you would be able to say "this collection of UUIDs came from the same device" or "this UUID came from this device". (See RFC 4122 for the various UUID formats and a discussion of this issue in section 4.5.)
If anonymity is a concern, running the UUID through a hash function, like SHA1 or MD5, would be good enough to make it unidentifiable.
Using a loop where you "add randomness" by mixing in random numbers is like shaking a dice for a few seconds versus an hour: the only difference is that you're rubbing sweat onto the dice.
As far as a locally unique string — which you could use as part of the file name — this is handy:
[[NSProcessInfo processInfo] globallyUniqueString]
I made a little program calculating gas consumption.
There is one view controller for each data entry necessary to calculate consumption in the final view controller.
So you enter the driven distance in the first controller, the gas fuelled up in the next controller. in the controller displaying the result, the current date is added, too.
All entries are stored in a plist file as strings for easier use with the tableviews later (except for the date).
Now I want to open this data in a summary view controller to populate a tableview.
Currently I have retrieve 3 arrays from the plist file (similar to the columns of a table):
array 1: NSDate
array 2: NSString representing a number
array 3: NSString representing a number
I know how to populate my tableview, but I cannot wrap my head around the sorting.
I tried changing the column approach into a table row based approach getting every single element from each of the arrays and adding them to a new array, containg each a date and two numbers (as strings).
I tried setting up sortdescriptors, I tried using selectors, but it seems I don't fully comprehend how to work it. Personally, I prefer examples, so I was looking for tutorials on this topic. The developer documentary didn't help that much.
I don't expect to get a complete example for this problem, but maybe some pointers on how to arrange my data and what best to use to sort the complete data array.
Thanks for any pointers or examples.
EDIT:
I finally got all "Table-lines" as "rows" into a dictionary. Now I can access the single columns by key for the column. And this also allows sorting the stringvalues in my array of dictionaries ascending or descending using a sortdescriptor.
This may not be the most elegant approach, but I think I now can do what I wanted.
Excuse the "Table" comparisons, but I find this easier to grasp.
If anyone has pointers to good array tutorials, I'd be more than thankful.
I'm not really clear on how you want to sort, but maybe you can adapt something from this (sorts an array of dictionaries each with a key #"row" with a value type NSNumber) -
First of all, make a comparison result function:
static NSComparisonResult compareRowNumbers(NSDictionary *dict1, NSDictionary *dict2, void * context) {
if ([[dict1 objectForKey:#"row"] intValue] < [[dict2 objectForKey:#"row"] intValue]) {return NSOrderedAscending;}
if ([[dict1 objectForKey:#"row"] intValue] > [[dict2 objectForKey:#"row"] intValue]) {return NSOrderedDescending;}
return NSOrderedSame;
}
Then you can use it like so -
NSMutableArray *myArray = [NSMutableArray array];
// populate your array with loads of stuff (in this case, NSDictionaries as decribed above)
// now sort it
[myArray sortUsingFunction:compareRowNumbers context:NULL];
...and your array is automagically sorted.
In dictionary named dict the key value pair is:
"alba\U2019s window" = "A model in westindies.";
And to send objectForKey: I am getting the string like "alba's window". When I send like following:
[dict objectForKey:alba's window];
I am not getting anything, it is showing null.
For starters, you might want to make that an actual string, like so:
[dict objectForKey:#"alba's window"];
Note also that \U2019 is not '; but ’, which is a different character entirely.
And more generally, sticking to [a-zA-Z0-9]+ for dictionary keys is probably a good idea, unless you are inserting and retrieving programmatically using the exact same string as a key.
Since iOS6 onwards, a convenient method for setting and accessing the object for a key from an NSDictionary is:
//Setting the object in NSMutableDictionary
dictionary[#"someKey"] = #"someString";
//Accessing the object
NSString *str = dictionary[#"someKey"];
Make sure your dict isn't null; sending a message to a null object will silently fail and return null.
Another way to check your key/values is to simply NSLog(#"%#",dict); and this will show you the contents of the dictionary. Note that this output only shows quotes around values when the value contains a space.
Also, make sure you're using the same pairs of strings as the key - it looks like you're using "alba\U2019s window" in addition to "alba's window".
I'm looking at using openUDID inside my app for registration purposes.
However its still using the UDID number apple issues and so I was just reading though the .m file and came across this:
// One day, this may no longer be allowed in iOS. When that is, just comment this line out.
//
#if TARGET_OS_IPHONE
if([UIDevice instancesRespondToSelector:#selector(uniqueIdentifier)]){
_openUDID = [[UIDevice currentDevice] uniqueIdentifier];
}
#endif
// Take this opportunity to give the simulator a proper UDID (i.e. nullify UDID and create an OpenUDID)
//
#if TARGET_IPHONE_SIMULATOR
_openUDID = nil;
#endif
// Next we try to use an alternative method which uses the host name, process ID, and a time stamp
// We then hash it with md5 to get 32 bytes, and then add 4 extra random bytes
// Collision is possible of course, but unlikely and suitable for most industry needs (e.g.. aggregate tracking)
//
However I'm not sure exactly what line to comment out so that it used the alternative method which uses the host name, process ID, and a time stamp
You would comment out the 3 lines of code in the TARGET_OS_IPHONE block
You can "comment" out the line with uniqueIdentifier in it by prefixing that line using double slash comment.... or you can change the #if TARGET_OS_IPHONE line to #if 0, which means that block of code will never get called at all.
And then the alternative code will get used instead.
The way the code is currently written, it seems pretty safe to me to just leave it as is. Once Apple does completely do away with the uniqueIdentifier method, the instancesRespondToSelector call will properly fail and the alternative code will get used automatically.
What would be the best way to name a file associated to a NSManagedObject. The NSManagedObject will hold the URL to this file.
But I need to create a unique filename for my file. Is there some kind of autoincrement id that I could use? Should I use mktemp (but it's not a temporary file) or try to convert the NSManagedObjectId to a filename? but I fear there will be special characters which might cause problem.
What would you suggest?
EDIT: I have a lot of these NSManagedObjects and each has its own image, so I want to generate a unique name for each picture.
You can use NSProcessInfo to generated the guid:
[[NSProcessInfo processInfo] globallyUniqueString]
And to reference a file I'd suggest just keeping the guid as NSManagedObject property and then just reference a file by that name from application support directory.
There is a good way to do this and one earlier answer almost had it – generate a GUID for each image instead of for the entire process. Call this method whenever you need a unique string, and then store it in the managed object:
+ (NSString *)getUUID
{
CFUUIDRef theUUID = CFUUIDCreate(NULL);
CFStringRef string = CFUUIDCreateString(NULL, theUUID);
CFRelease(theUUID);
return [(NSString *)string autorelease];
}
I use this for storing captured movies and images.
I thought about using the time to generate a unique filename but I don't like the solution, for instance if the time is reset or changed, there is a slight chance of getting two times the same filename.
I'm surprised not to find more information about this subject on the web.
Since iOS5 there is now the option to store large blobs in an external record file. CoreData decides whether or not to store the record in sqlite based on the size of the record.
Storing blobs in external location using built-in CoreData option