Here is my current code:
int i = 1;
NSString * StockOneYahooFinance = [NSString stringWithFormat:#"http://finance.yahoo.com/q/hp?s=S+Historical+Prices"];
NSString * PulledStockOne = [NSString stringWithContentsOfURL:[NSURL URLWithString:StockOneYahooFinance] encoding:1 error:nil];
for (i=1;i=30;i++){
NSString *StartPulling = [[PulledStockOne componentsSeparatedByString:#"nowrap align="] objectAtIndex:i];
NSString *StartOpen = [[StartPulling componentsSeparatedByString:#">"] objectAtIndex:3];
NSString *Open = [[StartOpen componentsSeparatedByString:#"<"] objectAtIndex:0];
NSString *StartClose = [[StartPulling componentsSeparatedByString:#">"] objectAtIndex:9];
NSString *Close = [[StartClose componentsSeparatedByString:#"<"] objectAtIndex:0];
year.text = Close;
i++;
}
But to the point I click the only button on the screen and it does exactly what I want it pulls the stocks open and close price for the day. But my current issue is I want it to pull all of these as an array so how can I do this?
First thing:
for (i=1;i=30;i++){
it should be:
for (i=1;i<=30;i++){
Second one, do not increment 'int i' value on the end of loop because 'for' loop already do this. For quick'n dirty way of debugging add:
NSLog(#"Current iteration: %i", i);
as the first function in 'for' loop to see what's happening there.
Related
I am getting server response as "We've organizations in following something locations. 1. First test 2. Second test 3. Third test 4. Fourth test. Choose one for more information";
I am working on Objective-C and trying to showing in UILabel, but issue is I want to show as Paragraph. Like splitting the string by following.
We've organizations in following something locations.
First test
Second test
Third test
Fourth test
Choose one for more information.
The above one is example, but the data is completely dynamic and no idea about how many points would be there in response string.
Anyone have idea about this.
Try this :
NSString *str = #"We've organizations in following something locations. 1. First test 2. Second test 3. Third test 4. Fourth test.";
str = [str stringByReplacingOccurrencesOfString:#"1." withString:#"\n 1."];
str = [str stringByReplacingOccurrencesOfString:#"2" withString:#"\n 2"];
str = [str stringByReplacingOccurrencesOfString:#"3" withString:#"\n 3"];
str = [str stringByReplacingOccurrencesOfString:#"4" withString:#"\n 4"];
yourlbl.text = str;
Dynamic way
NSString *str = #"We've organizations in following something locations. 1. First test 2. Second test 3. Third test 4. Fourth test.";
NSCharacterSet *numberCharset = [NSCharacterSet characterSetWithCharactersInString:#"0123456789"];
NSScanner *theScanner = [NSScanner scannerWithString:str];
while (![theScanner isAtEnd]) {
// Eat non-digits and negative sign
[theScanner scanUpToCharactersFromSet:numberCharset
intoString:NULL];
int aInt;
if ([theScanner scanInt:&aInt]) {
str = [str stringByReplacingOccurrencesOfString:[NSString stringWithFormat:#"%d",aInt] withString:[NSString stringWithFormat:#"\n %d",aInt]];
}
}
You can use like this
NSArray *array = [NSArray arrayWithObjects:#"First test",#"Second test",#"Third test" , #"Fourth test Choose one for more information.", nil];
__block NSString *strConcate = [NSString new];
[array enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
strConcate = [strConcate stringByAppendingString:[NSString stringWithFormat:#"%lu. %#\n",(unsigned long)(idx + 1),obj]];
}];
self.labelString.text = strConcate;
Finally, I found the solution, here it is. This may helps someone in future.
NSArray *pointsArray = [trimmedString componentsSeparatedByString:#"."];
NSString *strOccurence, *strReplacing;
for (int i=1; i<pointsArray.count; i++){
strOccurence = [NSString stringWithFormat:#"%d.",i];
strReplacing = [NSString stringWithFormat:#"\n%d.",i];
trimmedString = [trimmedString stringByReplacingOccurrencesOfString:strOccurence withString:strReplacing];
}
cell.textLabel.text = trimmedString;
![HERE is what my log is showing][1]I am working on a calculator and I have a function that takes in user input and then will display the answer just lie a normal calculator. I want to save any user input into a string such as "2+2=4" into an array which can then be viewed later.
Is there any way to convert the double values into strings? and also how would i save this string in the array.
Below is some code which i have tried, but have had no luck.
The below method is used to call the users input
NSString *leftString = [NSString stringWithFormat:#"%d + %d", left, right];
_array = [[NSArray alloc] initWithObjects: leftString, nil];
This:
NSString *leftString = [NSString stringWithFormat:#"%d", left "+" right];
should be like this:
NSString *leftString = [NSString stringWithFormat:#"%f + %f", left, right];
where the plus sign is within the expression and the %f indicates left and right are doubles.
Assuming you want to have an array like so: [left, operation symbol, right] then you do the following:
NSString *leftString = [NSString stringWithFormat:#"%f", left];
NSString *rightString = [NSString stringWithFormat:#"%f", right];
NSString *operationString= [NSString stringWithFormat:#"%#", operation];
Then do the following to add all of them into an array:
_array = [[NSArray alloc] initWithObjects: leftString, operation, rightString, nil];
Am having this nsstring
NSString * countryStr = #"6023117,159,en_US,Seychelles,SC,Seychelles,6023185,95,en_US,Kuwait,KW,Kuwait,6023182,172,en_US,Swaziland,SZ,Swaziland,6023185,157,en_US,Saudi Arabia,SA,Saudi Arabia,6023182,177,en_US,Tanzania,TZ,Tanzania,6023185,179,en_US,Togo,TG,Togo,6023185,87,en_US,Cote d'Ivoire,CI,Cote d'Ivoire";
now i want to display only the countries which are suffixed by "en_US".
can anybody tell me how to split that string to get the countries.
I did like this
NSError * error;
NSString* imageName = [[NSBundle mainBundle] pathForResource:#"CountryList" ofType:#"txt"];
NSString * countrStr = [NSString stringWithContentsOfFile:imageName encoding:NSStringEncodingConversionAllowLossy error:&error];
NSArray * dfd = [countrStr componentsSeparatedByString:#"en_US"];
for(int i=0;i<dfd.count;i++)
{
NSString * nama = [dfd objectAtIndex:1];
NSArray * djk = [nama componentsSeparatedByString:#","];
NSString * aksjd = [djk objectAtIndex:1];
}
You can do it like this;
NSString * countryStr = #"6023117,159,en_US,Seychelles,SC,Seychelles,6023185,95,en_US,Kuwait,KW,Kuwait,6023182,172,en_US,Swaziland,SZ,Swaziland,6023185,157,en_US,Saudi Arabia,SA,Saudi Arabia,6023182,177,en_US,Tanzania,TZ,Tanzania,6023185,179,en_US,Togo,TG,Togo,6023185,87,en_US,Cote d'Ivoire,CI,Cote d'Ivoire";
NSArray * arrData = [countryStr componentsSeparatedByString:#","];;//[countryStr componentsSeparatedByString:#"en_US"];
for(int i=0;i<arrData.count;i++)
{
NSString * str = [arrData objectAtIndex:i];
if ([str isEqualToString:#"en_US"] && i<arrData.count-1)
{
NSString* countryName = [arrData objectAtIndex:i+1];
NSLog(#"countryName %#", countryName);
}
}
But you should manage data in your file, loading from resource.
Best way to do it is
NSString *string = [NSString stringWithFormat: #"%#, %#, %#", var1, var2, var3];
Excuse the formatting/syntax errors. Typing this via iPhone. But if there is any errors, look up stringWithFormat: in iOS documents on the apple developer page for corrections.
Your string seems to have the following pattern:
A number,
An other number,
The country language code,
The name,
A short code,
An (other) name.
So what you can do is to use a loop like this:
for (int i = 0; i < dfd.count; i += 6) {
if ( dfd[i + 2] ) } // check the country code at index i + 2
// Do something
}
}
I've got a string that shows the stock amount using "-" as separators.
It's built up like this: localStock-wareHouseStock-supplierStock
Now I want to update the supplierStock at the end of the string, but as you can see in the code below it goes wrong when the original string returns more than a single-space value (such as 20).
Is there a way to remove all characters until the last "-" (or remove characters after the second "-")?
NSMutableString *string1 = [NSMutableString stringWithString: p1.colorStock];
NSLog(#"string1: %#",string1);
NSString *newString = [string1 substringToIndex:[string1 length]-2];
NSLog(#"newString: %#",newString);
NSString *colorStock = [NSString stringWithFormat:#"%#-%#",newString,p2.supplierStock];
NSLog(#"colorstock: %#",colorStock);
p1.colorStock = colorStock;
NSLog1
string1: 0-0-0
newString: 0-0
colorstock: 0-0-20
NSLog2
string1: 0-0-20
newString: 0-0-
colorstock: 0-0--20
EDIT: Got it working thanks to Srikar!
NSString *string1 = [NSString stringWithString: p1.colorStock];
NSLog(#"string1: %#",string1);
NSString *finalString = [string1 stringByReplacingOccurrencesOfString:[[string1 componentsSeparatedByString:#"-"] lastObject] withString:p2.supplierStock.stringValue];
NSLog(#"finalString: %#",finalString);
p1.colorStock = finalString;
Why not use componentsSeparatedByString followed by lastObject ?
NSString *supplierStock = [[string1 componentsSeparatedByString:#"-"] lastObject];
The above works if the "stock amount" is always in sets of 3's separated by a "-". Also since you always want supplierStock, lastObject is perfect for your needs.
Of course after splitting string1 with - you get a NSArray instance and you can access the individual components using objectAtIndex:index. So if you want localStock you can get by
NSString *localStock = [[string1 componentsSeparatedByString:#"-"] objectAtIndex:0];
I would suggest splitting the string into the 3 parts using [NSString componentsSeparatedByString:#"-"] and then building it back up again:
NSArray *components = [p1.colorStock componentsSeparatedByString:#"-"];
p1.colorStock = [NSString stringWithFormat:#"%#-%#-%#",
[components objectAtIndex:0],
[components objectAtIndex:1],
p2.supplierStock];
With a string that looks like
NSString *myString = #"Hello-World";
you can separate it with the componentsSeparatedByString: method of the NSString object as
NSArray *myWords = [myString componentsSeparatedByString:#"-"];
The myWords - array will then contain the two NSString objects Hello and World.
To access the strings:
NSString *theHelloString = [myWords objectAtIndex:0];
NSString *theWorldString = [myWords objectAtIndex:1];
Hope it helps!
None of these examples show how to do this if you are unaware of how many of these separator occurrences you're going to have in the original string.
Here's what I believe the correct the correct code should be for dismantling the original string and rebuilding it until you reach the final separator, regardless of how many separators it contains.
NSString *seperator = #" ";
NSString *everythingBeforeLastSeperator;
NSArray *stringComponents = [originalString componentsSeparatedByString:seperator];
if (stringComponents.count!=0) {
everythingBeforeLastSeperator = [stringComponents objectAtIndex:0];
for (int a = 1 ; a < (stringComponents.count - 1) ; a++) {
everythingBeforeLastSeperator = [NSString stringWithFormat:#"%#%#%#", everythingBeforeLastSeperator, seperator, [stringComponents objectAtIndex:a]];
}
}
return everythingBeforeLastSeperator;
I have read a few posts and found a method. I adapted it to my app, however at [self saveContext]; I get a bad instruction error.
Please let me know why, and if this method makes sense. Also I am using \r because in the csv each item is in its own row.
Thank you in advance
-(void)addData{
NSString *paths = [[NSBundle mainBundle] resourcePath];
NSString *bundlePath = [paths stringByAppendingPathComponent:#"coredatainfo.csv"];
NSString *dataFile = [[NSString alloc] initWithContentsOfFile:bundlePath];
NSArray *dataRows = [dataFile componentsSeparatedByString:#"\r"];
[dataFile release];
FirstCDitem *myItem;
for (int i = 0 ; i < [dataRows count] ; i++)
{
NSArray *dataElements = [[dataRows objectAtIndex:i] componentsSeparatedByString:#","];
NSLog(#"Added: %d %#",i,dataElements);
myItem = (FirstCDitem *)[NSEntityDescription insertNewObjectForEntityForName:#"FirstCDitem" inManagedObjectContext:[self managedObjectContext]];
[FirstCDitem setTitle:[dataElements objectAtIndex:i]];
[self saveContext];
}
}
There are two problems in this code line:
[FirstCDitem setTitle:[dataElements objectAtIndex:i]];
i is the current row number, so it probably makes no sense to use it as index to the dataElements array. Perhaps you want the first element in the row?
setTitle must sent to the instance myItem, not to the class FirstCDItem.
So the line should look like this:
[myItem setTitle:[dataElements objectAtIndex:0]];