How to display x raise to y in UIlabel - iphone

how can I display 5 raise to 1/3 in iphone i.e I want 1/3 written above 5 can anyone help please

I Found this solution, hope so it would be helpful for you.
x to the power of y in a UILabel could be easy. Just replace your indices with unicode superscript characters... I use the following method to turn an integer into a string with superscript characters.
+(NSString *)convertIntToSuperscript:(int)i
{
NSArray *array = [[NSArray alloc] initWithObjects:#"⁰", #"¹", #"²", #"³", #"⁴", #"⁵", #"⁶", #"⁷", #"⁸", #"⁹", nil];
if (i >= 0 && i <= 9) {
NSString *myString = [NSString stringWithFormat:#"%#", [array objectAtIndex:i]];
[array release];
return myString;
}
else {
NSString *base = [NSString stringWithFormat:#"%i", i];
NSMutableString *newString = [[NSMutableString alloc] init];
for (int b = 0; b<[base length]; b++) {
int temp = [[base substringWithRange:NSMakeRange(b, 1)] intValue];
[newString appendString:[array objectAtIndex:temp]];
}
[array release];
NSString *returnString = [NSString stringWithString:newString];
[newString release];
return returnString;
}
}

Try this NSString *cmsquare=#"cm\u00B2";
It will display cm².

Yes you can do that but you need custom UILabel, either Make it by yourself or Get it Open Source..

Related

How to convert Hex to Binary iphone

I need to convert a hex string to binary form in objective-c, Could someone please guide me?
For example if i have a hex string 7fefff78, i want to convert it to 1111111111011111111111101111000?
BR,
Suppi
Nice recursive solution...
NSString *hex = #"49cf3e";
NSUInteger hexAsInt;
[[NSScanner scannerWithString:hex] scanHexInt:&hexAsInt];
NSString *binary = [NSString stringWithFormat:#"%#", [self toBinary:hexAsInt]];
-(NSString *)toBinary:(NSUInteger)input
{
if (input == 1 || input == 0)
return [NSString stringWithFormat:#"%u", input];
return [NSString stringWithFormat:#"%#%u", [self toBinary:input / 2], input % 2];
}
Simply convert each digit one by one: 0 -> 0000, 7 -> 0111, F -> 1111, etc. A little lookup table could make this very concise.
The beauty of number bases that are powers of another base :-)
In case you need leading zeros, for example 18 returns 00011000 instead of 11000
-(NSString *)toBinary:(NSUInteger)input strLength:(int)length{
if (input == 1 || input == 0){
NSString *str=[NSString stringWithFormat:#"%u", input];
return str;
}
else {
NSString *str=[NSString stringWithFormat:#"%#%u", [self toBinary:input / 2 strLength:0], input % 2];
if(length>0){
int reqInt = length * 4;
for(int i= [str length];i < reqInt;i++){
str=[NSString stringWithFormat:#"%#%#",#"0",str];
}
}
return str;
}
}
NSString *hex = #"58";
NSUInteger hexAsInt;
[[NSScanner scannerWithString:hex] scanHexInt:&hexAsInt];
NSString *binary = [NSString stringWithFormat:#"%#", [self toBinary:hexAsInt strLength:[hex length]]];
NSLog(#"binario %#",binary);
I agree with kerrek SB's answer and tried this.
Its work for me.
+(NSString *)convertBinaryToHex:(NSString *) strBinary
{
NSString *strResult = #"";
NSDictionary *dictBinToHax = [[NSDictionary alloc] initWithObjectsAndKeys:
#"0",#"0000",
#"1",#"0001",
#"2",#"0010",
#"3",#"0011",
#"4",#"0100",
#"5",#"0101",
#"6",#"0110",
#"7",#"0111",
#"8",#"1000",
#"9",#"1001",
#"A",#"1010",
#"B",#"1011",
#"C",#"1100",
#"D",#"1101",
#"E",#"1110",
#"F",#"1111", nil];
for (int i = 0;i < [strBinary length]; i+=4)
{
NSString *strBinaryKey = [strBinary substringWithRange: NSMakeRange(i, 4)];
strResult = [NSString stringWithFormat:#"%#%#",strResult,[dictBinToHax valueForKey:strBinaryKey]];
}
return strResult;
}

how to remove quote and slash from string in iphone

Hi friends... I am using regular expression so I get string but with double quote and slash but I dont want that. I want string value without slash and double quotes. I try this I'm but not getting proper answer.
I get error after running application [/Users/pradeepyadav/Desktop/RegexKitLiteDemo/Classes/RegexKitLiteDemoAppDelegate.m:108:0 /Users/pradeepyadav/Desktop/RegexKitLiteDemo/Classes/RegexKitLiteDemoAppDelegate.m:108: error: incompatible block pointer types initializing 'void (^)(struct NSString *, NSUInteger, BOOL *)', expected 'void (^)(struct objc_object *, NSUInteger, BOOL *)
I get this error line
Second one is this : [/Users/pradeepyadav/Desktop/RegexKitLiteDemo/Classes/RegexKitLiteDemoAppDelegate.m:105:0 /Users/pradeepyadav/Desktop/RegexKitLiteDemo/Classes/RegexKitLiteDemoAppDelegate.m:105: warning: 'NSString' may not respond to '+stringByTrimmingCharactersInSet:
I get this error line [webData length] encoding:NSUTF8StringEncoding];
//NSLog(#"%#",loginStatus);
[connection release];
//
NSString *regexString = #"Stations\\[""(.*)""\\] = new Station\\((.*)new Array\\((.*)\\)\\);"; //#"Stations\\[""(.*)""\\] = new Station\\((.*)\\);"; //#"Stations\[""(.*)""\] = new Station\({[\,,2}(.*)new Array\((.*)\)\);"; //#"<a href=([^>]*)>([^>]*) - ";
matchArray = [loginStatus arrayOfCaptureComponentsMatchedByRegex:regexString];
NSMutableArray *newArray = [[NSMutableArray alloc] initWithCapacity:[matchArray count]];
//NSCharacterSet *charactersToRemove = [NSCharacterSet punctuationCharacterSet];
[matchArray enumerateObjectsUsingBlock:^(NSString *aString, NSUInteger idx, BOOL *stop)
{
NSString *newString = [NSString stringByTrimmingCharactersInSet:[NSCharacterSet punctuationCharacterSet]];//#############
[newArray insertObject:newString atIndex:idx];
NSLog(#"matchArray: %#", matchArray);
}];//******************
//NSLog(#"matchArray: %#", matchArray);
lstAirports = [[NSMutableArray alloc] initWithCapacity:[matchArray count]];
for (int i = 0; i < [matchArray count]; i++) {
airport *air=[[airport alloc]init];
//code
air.Code = [[matchArray objectAtIndex: i] objectAtIndex: 1];
NSLog(#"air.Code: %#\n",air.Code);
//name
NSString *temp=[[matchArray objectAtIndex: i] objectAtIndex: 2];
NSArray *arrParts=[temp componentsSeparatedByString:#""","];
//air.Name=arrParts[2];
air.Name=[arrParts objectAtIndex:2];
NSLog(#"air.Name: %#\n",air.Name);
//destination airports
temp=[[matchArray objectAtIndex: i] objectAtIndex: 3];
arrParts=[temp componentsSeparatedByString:#","];
air.DestinationAirports =arrParts;
NSLog(#"air.DestinationAirports: %#\n",air.DestinationAirports);
[lstAirports addObject: air];
NSLog(#"lstAirports: %#\n",lstAirports);
}
//[webData release];
}
please some help me fast it's vital for me
You don't need RegExp to remove occurrences of string in NSString.
See the example below, i hop it will help you:
NSString *str = #"fdf\"fdsfdsf\"fsdfsf/fsdfsdfsf\\fsdfsdf\\fsdffsd//fsdfsf\"fsdf/\\\"";
str = [str stringByReplacingOccurrencesOfString:#"\"" withString:#""];
str = [str stringByReplacingOccurrencesOfString:#"\\" withString:#""];
str = [str stringByReplacingOccurrencesOfString:#"/" withString:#""];
NSLog(#"%#", str);
Check for the usage of
stringByReplacingOccurrencesOfString:withString:
in
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html.
You can replace them with "" characters.

extract the first character

i have for exemple "My String" i want to extract the first character .
String *_initialeStr = self.carte.Titre ;
originalCarte.Init = memmove(_initialeStr , _initialeStr+1,length(_initialeStr));
NSString has characterAtIndex: method where you can pass 0 as index...
Use characterAtIndex: from NSString class.
- (unichar)characterAtIndex:(NSUInteger)index
Use as below .
NSString *temp = #"Hello world";
unichar myCharacter = [temp characterAtIndex:0];
That doesn't even look like Objective-C.
Having a property named Init is quite confusing and what is String?
Perhaps this is what you want:
NSString *initialString = self.carte.titre;
originalCarte.initialLetter = [initialString characterAtIndex:0];
This outputs the first character of myString:
NSString *myString = #"The text I want to access.";
NSMutableArray *myArray = [[NSMutableArray alloc] initWithCapacity:[myString length]];
for ( i = 0; i < [myString length]; i++ )
[myArray addObject:[NSNumber numberWithChar:[myString characterAtIndex:i]]];
NSLog( #"First character: %c", [[myArray objectAtIndex:0] charValue] );

convert a char to string

my code works great until know and, if I put a double digit number into the text field (like 12) nslog returns 2 single digit numbers (like 1 and 2). Now I need to put these 2 single digit numbers into 2 strings. can somebody help me. thanks in advance.
NSString *depositOverTotalRwy = [NSString stringWithFormat:#"%#", [deposit text]];
NSArray *components = [depositOverTotalRwy
componentsSeparatedByString:#"/"];
NSString *firstThird = [components objectAtIndex:0];
for(int i = 0; i < [firstThird length]; i++)
{
char extractedChar = [firstThird characterAtIndex:i];
NSLog(#"%c", extractedChar);
}
You should be able to use -stringWithFormat:.
NSString *s = [NSString stringWithFormat:#"%c", extractedChar];
EDIT:
You can store them in an array.
NSMutableArray *digits = [NSMutableArray array];
for ( int i = 0; i < [s length]; i++ ) {
char extractedChar = [s characterAtIndex:i];
[digits addObject:[NSString stringWithFormat:#"%c", extractedChar]];
}
Try to print the value of firstThird using NSLog(), see what it exactly hold, you code seem correct,
Use characterAtIndex function for NSString to extract a character at known location
- (unichar)characterAtIndex:(NSUInteger)index
Use as below
NSString *FirstDigit = [NSString stringWithFormat:#"%c", [myString characterAtIndex:0]];
NSString *SecondDigit = [NSString stringWithFormat:#"%c", [myString characterAtIndex:1]];

Memory Leak according to Instruments

Been running instruments on my app. Its says i am leaking 864bytes & 624bytes from 2 NSCFString and the library responsible is Foundation.
So that leads me to believe thats its not a leak caused by me? Or is it?
Here is the offending method according to instruments. It seems to be a
substringWithRange
that is leaking.
-(void) loadDeckData
{
deckArray =[[NSMutableArray alloc] init];
NSString* path = [[NSBundle mainBundle] pathForResource:#"rugby" ofType:#"txt"
inDirectory:#""];
NSString* data = [NSString stringWithContentsOfFile:path encoding:
NSUTF8StringEncoding error: NULL];
NSString *newString = #"";
NSString *newline = #"\n";
NSString *comma = #",";
int commaCount = 0;
int rangeCount = 0;
NSString *nameHolder = #"";
NSString *infoHolder = #"";
NSMutableArray *statsHolder = [[NSMutableArray alloc] init];
for (int i=0; i<data.length; i++)
{
newString = [data substringWithRange:NSMakeRange(i, 1)];
if ([newString isEqualToString: comma]) //if we find a comma
{
if (commaCount == 0)// if it was the first comma we are parsing the
NAME
{
nameHolder = [data substringWithRange:NSMakeRange(i-
rangeCount, rangeCount)];
}
else if (commaCount == 1)//
{
infoHolder = [data substringWithRange:NSMakeRange(i-
rangeCount, rangeCount)];
//NSLog(infoHolder);
}
else // if we are on to 2nd,3rd,nth comma we are parsing stats
{
NSInteger theValue = [[data
substringWithRange:NSMakeRange(i-rangeCount,rangeCount)]
integerValue];
NSNumber* boxedValue = [NSNumber
numberWithInteger:theValue];
[statsHolder addObject:boxedValue];
}
rangeCount=0;
commaCount++;
}
else if ([newString isEqualToString: newline])
{
NSInteger theValue = [[data substringWithRange:NSMakeRange(i-
rangeCount,rangeCount)] integerValue];
NSNumber* boxedValue = [NSNumber numberWithInteger:theValue];
[statsHolder addObject:boxedValue];
commaCount=0;
rangeCount=0;
Card *myCard = [[Card alloc] init];
myCard.name = nameHolder;
myCard.information = infoHolder;
for (int x = 0; x < [statsHolder count]; x++)
{
[myCard.statsArray addObject:[statsHolder
objectAtIndex:x]];
}
[deckArray addObject:myCard];
[myCard autorelease];
[statsHolder removeAllObjects];
}
else
{
rangeCount++;
}
}
[statsHolder autorelease];
}
Thanks for your advice.
-Code
As Gary's comment suggests this is very difficult to diagnose based on your question.
It's almost certainly a leak caused by you however, I'm afraid.
If you go to the View menu you can open the Extended Detail. This should allow you to view a stack trace of exactly where the leak occurred. This should help diagnose the problem.
When to release deckArray? If deckArray is a class member variable and not nil, should it be released before allocate and initialize memory space?