Converting a time_t to NSDate object? - nsdate

Is there any method provided by Apple or any technique that can convert a time_t type variable to an NSDate object?

NSDate *someDate = [NSDate dateWithTimeIntervalSince1970:some_time_t]

Related

How to convert String^ to String in C++ CLI?

Whenever I try to directly assign a String variable to another String variable I get error found no suitable conversation.
So is there a way to convert String^ pointer to a non-pointer struct String ?
I want :
System::String a = System::String('X',256);
I don't want :
System::String^ a = %System::String('X',256);
No, there is not, because as Hans pointed out in a comment, System::String is immutable. You cannot assign it. You can only associate a handle with an entirely new System::String object.
BTW
System::String^ a = %System::String('X',256);
is incorrect, it should be
System::String^ a = gcnew System::String('X',256);
Use System::String a('X', 256);.

NSString from NSData with the character: fl

How can I return back to an NSString from the byte date of the NSString containing this character: fl ?
NSString *inflatedString01 = #"fl";
// original code that was the problem!
NSData *dataOfString = [NSData dataWithBytes:[inflatedString01 UTF8String] length:[inflatedString length]];
// code that fixes the problem
//NSData *dataOfString = [inflatedString01 dataUsingEncoding:NSUTF8StringEncoding]; //thanks zneak
NSLog(#"%#",inflatedString01);
NSLog(#"%i",[inflatedString01 length]);
NSLog(#"%#",dataOfString);
NSLog(#"%i",[dataOfString length]);
NSString *stringFromData = [NSString stringWithCString:[dataOfString bytes] encoding:NSUTF8StringEncoding]
NSLog(#"%#",stringFromData);
The output of the above gives:
2012-01-02 08:47:48.963 TestApp[74363:fe03] fl
2012-01-02 08:47:49.262 TestApp[74363:fe03] 1
2012-01-02 08:47:49.540 TestApp[74363:fe03] <ef>
2012-01-02 08:47:49.924 TestApp[74363:fe03] 1
2012-01-02 08:47:50.787 TestApp[74363:fe03] (null)
I'd like to see fl instead of (null) for the last NSLog output. I'm guessing there is a significance with the 'ef' output of NSData.
The problem is the discrepancy of the character size versus the binary size. When you call [NSString length], what you get is the number of logical characters in the string, not the number of bytes required to store it in an arbitrary encoding. The character fl is one logical character for the NSString class, but its UTF-8 encoding is ef ac 82: it takes up 3 bytes.
Your call to [NSData dataWithBytes:length:] receives a pointer to these 3 bytes, but then [inflatedString01 length] tells that it's only one character and you pass that as the number of bytes; this is why your data contains only ef.
strlen, not being encoding-aware, will just count the number of bytes in a C string until it finds a zero, so it will accurately return the number of bytes in an UTF-8 string (as you noted in your comment).
The best solution would probably to simply call [inflatedString01 dataUsingEncoding:NSUTF8StringEncoding] to get the bytes.

convert ByteArray to NSData

I want convert ByteArray into NSData
How can i do?
please Help me
I assume what you're calling a ByteArray is actually a "char *", or more generally a pointer to some data.
Then you'll want to do [NSData dataWithBytes:pointer]. Read http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/BinaryData/Tasks/WorkingBinaryData.html

MemoryLeaking from array with dictionaries

I have my function getAllData, which is returning an array with dictionaries.
- (NSArray *)getAllData {
NSMutableArray *result = [[NSMutableArray alloc] init];
NSArray *data = [skiResorts sortedArrayUsingFunction:comparator context:NULL];
NSString *currentLetter = #"A";
NSMutableArray *array = [[NSMutableArray alloc] init] ;
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init] ;
if ([data count] > 0) {
for (SkiResort *resort in data) {
if ([resort.name hasPrefix:currentLetter]) {
// Same letter as before.
// Add current SkiResort to temporary array.
[array addObject:resort];
} else {
// New letter.
// Add previous header/row data to dictionary.
[dict setValue:currentLetter forKey:#"header"];
[dict setValue:array forKey:#"row"];
// Add dictionary to final result array.
[result addObject:dict];
// Startover ...
[array removeAllObjects];
[dict removeAllObjects];
// Prepare for next letter.
currentLetter = [resort.name substringToIndex:1];
// Add current SkiResort to temporary array.
[array addObject:resort];
}
}
// Add previous header/row data to dictionary.
[dict setValue:currentLetter forKey:#"header"];
[dict setValue:array forKey:#"row"];
// Add dictionary to final result array.
[result addObject:dict];
}
[array release];
[dict release];
return [result autorelease];
}
Can anyone see obvious memory leaks in my code? I get memory leak array, dict, and result...
From the code, I have to ask: you're aware that addObject: doesn't copy the object? So setting values to dict, then adding it to result, then removing everything from dict just leaves an empty dictionary in result? I think you probably want to use the 'copy' method in there, to make copies of the array and dictionary. Or, even better, just create the dictionary when you add it to result using one of the class methods.
Anyway, since I can't see any leaks in that, much more likely is that whoever receives the result of getAllData subsequently leaks it. If for some crazy reason you had somewhere a stray:
[[object getAllData] retain];
Then the leaks tool would identify a leak of array, dict and result and point you to getAllData as the method in which they were originally created.
I really should say before I answer my knowledge on this is VERY limited, so take that into consideration. But it seems to me like you have a container inside a container. And when u reserve memory for that you have to backwards unreserve it.
For example, Store n (B_Containers) in Container A reserves n_B containers and 1 A container. To unreserve the memory you need to unreserve n B_Containers first before you delete A or all B_Containers will be withou reference and floating around in your memory.

How to convert NSString to NSNumber without using numberfromstring

I want to convert NSString to NSNumber without using numbreFromString: method? The numbreFromString: metgod is acting weirdly in 4.0. So i want to use an alternative to convert NSString to NSNumber. Please Help me....
+ (NSString *)formatText:(NSString *) text withLocalSettings:(BOOL) isLacale {
NSNumber *aNsNumber= [numberFormatter numberFromString: text];
NSString *returnString = [NSString stringWithString:[numberFormatter stringForObjectValue:aNsNumber]];
if(isLacale) {
return returnString;
}
else {
return [NSString stringWithFormat:#"%lf",[aNsNumber doubleValue]];
}
}
0
I am developing an application i need to run my app both in 3.0 and 4.0. I have a textfield where when i try to enter numbers in the textfield the behaviour is like this... IN 3.0 :- It allows to enter 7 digits and 2 fractional values (I have formatted it like this). I have formatted and localized the numbers along with the comma seperations depending on the country selected. It is working perfectly in 3.0 and 3.1.2
IN 4.0 : - It allows you to enter only 4 numbers and after entering 5th digit it is making the textfields empty.. Nothing is displayed when u enter the 5th number and when u enter the 6th number it starts from the 1st number and continues the same til 4 numbers. ex: - when u enter 1234, textfield appears - 1234 and when u enter 12345, textfield appears " ". and when u enter 6 now it starts with 6 and so on..
I am using the NSNumberFormatter and numberfromstring method to format the values entered in the textfield.
I am not able to understand why this is happening like this... Please help me...
[NSNumber numberWithInteger:[theString integerValue]];
[NSNumber numberWithDouble:[theString doubleValue]];
also floatValue, intValue, longLongValue, boolValue
Edit:
to strip out commas, use stringByReplacingOccurrencesOfString before doing the above.
theString = [theString stringByReplacingOccurrencesOfString:#"," withString:#""];
to strip out more than one character, you could get something working with componentsSeparatedByCharactersInSet.
theSet = [NSCharacterSet characterSetWith...];
theString = [[theString componentsSeparatedByCharactersInSet:theSet] componentsJoinedByString:#""];
iOS4 is very picky when it comes to formatting of numbers.
In your example code you pass in a locale - make sure that the decimal separator in your string is in line with what is defined in the locale. Make sure that you enter numbers that are correctly formatted (iOS4) id est nine thousand should be 9.000 and not 9000 if your are using DecimalStyle.
If you insist on using an alternative parser - sscanf an atoi could be an option.
Use standard c lib's sscanf with formatter or atoi depending on the numbers you need to convert.
#include<stdio.h>
int main(int argc, char **argv) {
char s[] = "1.2";
double x;
sscanf(s, "%lf", &x);
printf("The decimal number was %g\n\n", x);
return 1;
}
atoi takes a string containing an integer and returns the value as an int.

Resources