I have an NSArray with various parts divided by ,.
this array is much longer
citiesArray10000 = [NSArray arrayWithObjects:
#"33.572162&-112.087966&Phoenix&Arizona",
#"32.154289&-110.871062&735&Tucson Arizona ",
#"33.401926&-111.717379&Mesa&Arizona",
#"33.282874&-111.854943&Chandler&Arizona",
nil];
I loop through these to see if they meet certain conditions.
[citiesArray10000 enumerateObjectsUsingBlock:^(id object, NSUInteger idx, BOOL *stop) {
// do something with object
NSArray *coorArray = [object componentsSeparatedByString:#"&"];
NSString *firstString = [coorArray objectAtIndex:0];
NSString *secondString = [coorArray objectAtIndex:1];
NSString *thirdString = [coorArray objectAtIndex:2];
NSString *fourthString = [coorArray objectAtIndex:3];
if (fabs(crnLoc.coordinate.latitude - latitude) <= 1) {
if (abs(crnLoc.coordinate.longitude - longitude <= 1)) {
self.label.text = fourthString;
}
}
the labels will float on the surface of the screen depending on the coordinates of the object that meet the conditions
self.label.frame = CGRectMake(160,(((self.mheading-90)-β)*-5.688)+200, 30, 200);
self.label2.frame = CGRectMake(160,(((self.mheading-90)-β)*-5.688)+200, 30, 200);
where β has different values depending on the coordinates.
the problem is that if more than one object in the array meets the conditions I need to create another label and have it's text be the fourthString of that object. Then when the condition isn't met any more delete that label. Is there anyway to do this?
How about using tag?
When you iterating through the citiesArray10000 array, I think you can add a tag to the uilabel and later on, when the conditions not meet anymore, you can find the label by the tag and delete the label.
[citiesArray10000 enumerateObjectsUsingBlock:^(id object, NSUInteger idx, BOOL *stop) {
// do something with object
NSArray *coorArray = [object componentsSeparatedByString:#"&"];
NSString *firstString = [coorArray objectAtIndex:0];
NSString *secondString = [coorArray objectAtIndex:1];
NSString *thirdString = [coorArray objectAtIndex:2];
NSString *fourthString = [coorArray objectAtIndex:3];
if (fabs(crnLoc.coordinate.latitude - latitude) <= 1) {
if (abs(crnLoc.coordinate.longitude - longitude <= 1)) {
UILabel *label = (UILabel *)[self.view viewWithTag:idx];
label.text = fourthString;
// add the label to the view ...
}
}
}
Related
I have a UITextField as following:
_itemTextField = [[UITextField alloc];
This UITextField may contain item number only ex: "11321" or item number and size ex: "11321-XS" or "12355-40".
I want to extract the item and size (if it's available) separately in two different variables, as following:
NSString *itemNumber = #"11321";
NSString *itemSize = #"XS";
Please advise.
NSString *str = #"11115-ex";
if([str containsString:#"-"]){
NSArray * arr =[str componentsSeparatedByString:#"-"];
NSString *first = [arr objectAtIndex:0];
NSString *second = [arr objectAtIndex:1];
NSLog(#"Return String %# %#",first,second);
}
else
{
NSLog(#"Return String %#",str);
}
I am using SQLite and I want to save the name, address, and phone text fields for them to show up in the next view controller for when the "show details" button is clicked in 1st VC.
I placed "save" and "show details" button in 1st VC, as well as "previous" and "next" button in 2nd VC. Whenever I click on "show details" I am getting this error message:
index 0 beyond bounds for empty array.
However, I see that the array is not empty. I want to store the student details in the array.
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *homeDirectory = NSHomeDirectory();
NSString *documentsDirectoryPath = [homeDirectory stringByAppendingPathComponent:#"Documents"];
self.dbFilePathInDocuments = [documentsDirectoryPath stringByAppendingPathComponent:#"details.db"];
self.studentDetails = [[NSMutableArray alloc]init];
NSString *selectQuery = [NSString stringWithFormat:#"select name,address,phone from contacts"];
sqlite3_open([self.dbFilePathInDocuments UTF8String], &dataBase);
sqlite3_prepare_v2(dataBase, [selectQuery UTF8String], -1,&selectStatement, NULL);
while (sqlite3_step(selectStatement) == SQLITE_ROW)
{
NSMutableDictionary *studentDict = [[NSMutableDictionary alloc]init];
NSString *name = [NSString stringWithFormat:#"%s",sqlite3_column_text(selectStatement, 0)];
NSString *address = [NSString stringWithFormat:#"%s",sqlite3_column_text(selectStatement, 1)];
NSString *phone = [NSString stringWithFormat:#"%s",sqlite3_column_text(selectStatement, 2)];
[studentDict setObject:name forKey:#"name"];
[studentDict setObject:address forKey:#"address"];
[studentDict setObject:phone forKey:#"phone"];
[self.studentDetails addObject:studentDict];
NSLog(#"student is:%#",self.studentDetails);
}
sqlite3_finalize(selectStatement);
sqlite3_close(dataBase);
self.nameLabel.text = [[self.studentDetails objectAtIndex:0] valueForKey:#"name"];
self.addressLabel.text = [[self.studentDetails objectAtIndex:0] valueForKey:#"address"];
self.phoneLabel.text = [[self.studentDetails objectAtIndex:0] valueForKey:#"phone"];
currentStudentIndex = 0;
}
- (IBAction)clickPrevious:(id)sender {
if(currentStudentIndex <=0)
{
currentStudentIndex = 0;
}else
{
currentStudentIndex = currentStudentIndex - 1;
}
self.nameLabel.text = [[self.studentDetails objectAtIndex:currentStudentIndex] valueForKey:#"name"];
self.addressLabel.text = [[self.studentDetails objectAtIndex:currentStudentIndex] valueForKey:#"address"];
self.phoneLabel.text = [[self.studentDetails objectAtIndex:currentStudentIndex] valueForKey:#"phone"];
}
- (IBAction)clickNext:(id)sender {
if(currentStudentIndex >= [self.studentDetails count] - 1)
{
currentStudentIndex = [self.studentDetails count] - 1;
}else
{
currentStudentIndex = currentStudentIndex + 1;
}
self.nameLabel.text = [[self.studentDetails objectAtIndex:currentStudentIndex] valueForKey:#"name"];
self.addressLabel.text = [[self.studentDetails objectAtIndex:currentStudentIndex] valueForKey:#"address"];
self.phoneLabel.text = [[self.studentDetails objectAtIndex:currentStudentIndex] valueForKey:#"phone"];
}
The issue is that you always accessing the array self.studentDetails even if it's empty. This will cause an exception.
First limit setting of the labels to a single method and check the array access will succeed before attempting it:
- (void)updateLabels
{
if (currentStudentIndex >= [self.studentDetails count])
return;
self.nameLabel.text = [[self.studentDetails objectAtIndex:currentStudentIndex] valueForKey:#"name"];
self.addressLabel.text = [[self.studentDetails objectAtIndex:currentStudentIndex] valueForKey:#"address"];
self.phoneLabel.text = [[self.studentDetails objectAtIndex:currentStudentIndex] valueForKey:#"phone"];
}
and use that method in the 3 places you currently set the labels. For example:
- (IBAction)clickPrevious:(id)sender {
currentStudentIndex--;
[self updateLabels];
}
- (IBAction)clickNext:(id)sender {
currentStudentIndex++;
[self updateLabels];
}
In the viewDidLoad method use this code:
...
sqlite3_finalize(selectStatement);
sqlite3_close(dataBase);
currentStudentIndex = 0;
[self updateLabels];
After that you're gonna want to work on enabling/disabling buttons depending on whether there is a next or previous student to view to make using the app more intuitive.
Data I need to displayed has been stored into a temp file.When I execute the application, NSLog can receive the data, but UITextField didn't respond. I don't know if I am using the correct syntax. And I am not sure why it doesn't work...
Update: I tried to assign a value to UItextField, but the UItextField doesn't display it neither
-(void)readFilewin
{
// ....
NSString *pathsWin;
NSArray *pathsWin1= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
pathsWin = [[pathsWin1 objectAtIndex:0] stringByAppendingPathComponent:#"arraycountwin.plist"];
NSArray *getfile = [NSArray arrayWithContentsOfFile:pathsWin];
//NSArray *getfile = [NSArray arrayWithContentsOfFile:#"arraycountwin.plist"];
//self.TotalWinTextField.text = [getfile objectAtIndex:0];
NSString * win = [[NSString alloc]init];
win = [getfile objectAtIndex:0];
NSLog(#"Win : %#", win);
self.TotalWinTextField.text = [NSString stringWithFormat:#"%#", win];
}
-(void)readFileLoss{...}
-(void)readFilestats
{
// ....
NSString *statspath;
NSArray *statspaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
statspath = [[statspaths objectAtIndex:0] stringByAppendingPathComponent:#"arraystats.plist"];
NSArray *getfile = [NSArray arrayWithContentsOfFile:statspath];
NSString * time = [[NSString alloc]init];
time = [getfile objectAtIndex:0];
NSLog(#"Time : %#", time);
NSString * time1 = [[NSString alloc]init];
time1 = [getfile objectAtIndex:1];
NSLog(#"Time1 : %#", time1);
self.textField1.text = time; //broke here after update the viewDidLoad as #paul11 advise
self.textField2.text = time1;
}
Update for testing purpose to make sure the textfield is working, but it cannot even read the assigned value:
- (void)readFilewin
{
self.TotalWinTextField.text = #"23"; //DONT WORK
}
I think you forgot to reference UITextField in the storyboard with your property
#property (weak, nonatomic) IBOutlet UITextField *totalWinTextField;
that is easy you just need to select your UITextField in the storyboard holding down the key control and you drag it to your property. You can see when the property is referenced when in the left side has a circle filled.
I hope to be helpful.
From your comment, this is your viewDidLoad -
- (void)viewDidLoad {
//win
self.TotalWinTextField = 0;
//loss
self.TotalLossTextField = 0;
//stats
self.textField1 = 0;
self.textField2 = 0;
self.textField3 = 0;
[self readFilewin];
[self readFilelose];
[self readFilestats];
}
This is setting all of your IBOutlets to nil (0).
I presume that what you meant was -
- (void)viewDidLoad {
//win
self.TotalWinTextField.text = #"0";
//loss
self.TotalLossTextField.text = #"0";
//stats
self.textField1.text = #"0";
self.textField2.text = #"0";
self.textField3.text = #"0";
[self readFilewin];
[self readFilelose];
[self readFilestats];
}
First of all make sure that outlet is created correctly
Secondly, check it is properly synthesized
thridly, make sure outlet is nonatomic
change ur viewDidLoad to this
- (void)viewDidLoad {
// Do any additional setup after loading the view.
[super viewDidLoad];
//win self.TotalWinTextField = 0;
//loss self.TotalLossTextField = 0;
//stats self.textField1 = 0;
self.textField2 = 0;
self.textField3 = 0;
[self readFilewin];
[self readFilelose];
[self readFilestats];
}
So i want to print the users in an NSMutableArray. But the strings keep coming out as nil.
here is what i have:
int users = 0;
- (IBAction)addNewUser:(id)sender {
NSString *string;
string = userNameTextField.text;
[usernameArray insertObject:string atIndex:users];
users++;
[self showUsers];
}
-(void)showUsers{
for (int i = 0; i < users; i++){
NSString *s = textView.text;
NSString *add;
add = [NSString stringWithFormat:#"%# ",[usernameArray objectAtIndex:i]];
NSString *display = [NSString stringWithFormat:#"%# \n %#", s, add];
textView.text = display;
}
}
i have also tried
-(void)showUsers{
for (int i = 1; i < users; i++){
NSString *s = textView.text;
NSString *add;
add = [usernameArray objectAtIndex:i];
NSString *display = [NSString stringWithFormat:#"%# \n %#", s, add];
textView.text = display;
}
}
First of all try using more comprehensive names for the objects. I'm rewriting your code.
Common Causes for the problem : Array not initialized, you are starting your for cycle with int i equal to 1, so you are missing the object at index 0 at your mutable array. Try the following code.
#interface InterfaceName : InterfaceInherits <IfDelegate> {
int usersCount;
NSMutableArray * usernameArray;
}
#implementation InterfaceName
/*There's no more confident way to initialize a variable than in the init method of the class. */
-(id)init{
usersCount = 0;
//You have to be sure that your array is not nil
usernameArray = [NSMutableArray alloc]init]];
return self;
}
- (IBAction)addNewUser:(id)sender {
NSString *username = [usernameTextField text];
[usernameArray insertObject:username atIndex:usersCount];
usersCount++;
//I'll omit the display as I'm not sure what you were doing with it.
}
-(void)showUsers{
for (int i = 0; i < usersCount; i++){
NSString *retrievedUser = [usernameArray objectAtIndex:i];
NSString *display = [NSString stringWithFormat:#"User Retrieved : %#",retrievedUser];
textView.text = display;
}
}
#end
I have a string like this
12,23,45,3,12,
What I want to do is get this each number and check with an array value. How I can get each value as a substring to check
Thanks
Break this string to array.
NSString *string = #"12,23,45,3,12,";
NSArray *array = [string componentsSeparatedByString:#","];
Then you can compare with the array.
EDIT :
As per your comment that you want to check all the string values to be present in main-other-array.
NSString *string = #"12,23,45,3,12";
NSArray *array = [string componentsSeparatedByString:#","];
//below is the main-other-array
NSArray *toCheckArray = #[#"124",#"23",#"45",#"3",#"12",#"1000"];
BOOL arrayIsContainedInToCheckArray = YES;
for (NSString *arrayObj in array) {
if (![toCheckArray containsObject:arrayObj]) {
arrayIsContainedInToCheckArray = NO;
}
}
NSLog(#"%#",arrayIsContainedInToCheckArray?#"All exist":#"All doesn't exist");
May be it helps you :
NSString *str = #"12,23,45,3,12";
NSArray *strArray = [str componentsSeparatedByString:#","];
NSArray * anotherArray = nil; // have some value
for (NSString * value in strArray)
{
int intVal = [value integerValue]; // here is your separate value
for (int i = 0; i < [anotherArray count]; i++) // You can check against another array
{
id anotherVal = [anotherArray objectAtIndex:i];
// Here you can check intVal and anotherVal from another array
}
}
Use this, It will help you..
NSArray *detailArray = [yourString componentsSeparatedByString:#","];