It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have a NSMutableArray with comma separated strings for an objective-c iphone application (iOS SDK 6.0). I need a simple function that, when triggered, returns a string, one at a time, from string 0 onwards. To give you some context, a user would click a button, and for every click a new string is returned, in order, from the Array. It's a list of previously saved "favourite quotes". The string is displayed in a UITextView.
Ideally, I would also have a function for reversing, i.e. going backwards in the array from the current position.
This might be pretty basic, but I seem to only be able to find more advanced implementations that I'm unable to translate into this looping backwards and forwards in an Array of strings.
EDIT: Current code for this function below. I need to add the part where one string at a time is returned from the array (allRows) and displayed in a textview
- (IBAction)nextQoute:(id)sender {
const char *dbpath = [_databasePath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &_qoutesDB) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat:
#"SELECT qoutesSaved FROM qoutes"];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(_qoutesDB,
query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
NSMutableArray *allRows = [[[NSMutableArray alloc] init] autorelease];
while (sqlite3_step(statement) == SQLITE_ROW)
{
NSString *qouteField = [[NSString alloc]
initWithUTF8String:
(const char *) sqlite3_column_text(
statement, 0)];
NSString *str = [NSString stringWithFormat:#"%#", qouteField];
[allRows addObject:str];
[qouteField release];
}
Very thankful for help!
You mention that you are new to Objective C, and based on how I read what you're describing, I wonder if you are making this a lot more complicated than it needs to be. Items in an array (that is, NSArray, or NSMutableArray) aren't "seperated" by anything - different items in an array are accessed by their order in the array, called an index. So in your code, when you build allRows, each str that you put into it gets put into its own index, and to get it back out of the array, you just use that index.
For example, let's pretend your quotes you are pulling from your database are:
"Here's looking at you kid"
"I'll be back"
"It's a trap"
If they are put into the array in that order, and you want to put "It's a trap" into a textfield (called myTextField), you just write
myTextField.text = [allRows objectAtIndex:2];
In the end, that means what you probably want to do for your app is keep a counter you pass to objectAtIndex. When the user clicks the forward button, increase the count. When they click the back button, decrease the count. Then, call the code I put above, except instead of sending the value 2, send your counter variable. As mentioned in the comments to H2CO3s answer, make sure you put some validation in there to prevent your count from going beyond the limits of your array, or you'll get a nasty crash. If this is indeed what you are trying to do and you are still confused, I can add some more code.
I'm not too familiar with accessing a sqlite database the way you do here (I use CoreData, so the calls are very different), so perhaps I am completely mistaken about what you are trying to do here - but this seems like a very simple task that is being very overcomplicated.
You can just keep track of the string index in an instance variable (or if you don't have an object to work with because you write a class method or a function and not an instance method, then you can use a static local variable too).
#interface Foo: NSObject {
NSInteger index;
NSArray *strings;
}
// ...
- (NSString *)nextString
{
return index < strings.count ? strings[index++] : nil;
}
- (NSString *)previousString
{
return index > 0 ? strings[--index] : nil;
}
You can fetch the whole data in database.And store it one array
declare these two objects.
#interface YourClassName: NSObject {
NSInteger stringIndex;
NSArray *quoteFieldDataArr;
}
NSMutableArr *quoteFieldDataArr=[self fetchDataFromDataBase];
-(NSMutableDictionary *)fetchDataFromDataBase
{
const char *dbpath = [_databasePath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &_qoutesDB) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat:
#"SELECT qoutesSaved FROM qoutes"];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(_qoutesDB,
query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
NSMutableDictionary *allRows = [[[NSMutableDictionary alloc] init] autorelease];
while (sqlite3_step(statement) == SQLITE_ROW)
{
NSString *qouteField = [[NSString alloc]
initWithUTF8String:
(const char *) sqlite3_column_text(
statement, 0)];
NSString *str = [NSString stringWithFormat:#"%#", qouteField];
[allRows addObject:str];
[qouteField release];
return nil;
}
// Load Next String
- (NSString *)loadNextString
{
return stringIndex < quoteFieldDataArr.count ? quoteFieldDataArr[stringIndex++] : nil;
}
- (NSString *)loadpreviousString
{
return stringIndex > 0 ? quoteFieldDataArr[--stringIndex] : nil;
}
Related
Since some months I am interested in coding. At the moment I am working on a programm which saves informations from textfields in a sqlite3 database. I am programming this project on Objective C in Xcode for Mac. Unfortunately I have problems in inserting data into my database. When I insert it and open my database in Terminal I just get this information in the cell for the string:
Here ist my code:
NSString *Bt = [NSString stringWithFormat:#"%#", Text]; //Theres a IBOutlet NSTextField *Text; in the Header data of the class
dbPath=#"/Users/Desktop/database.db";
if (sqlite3_open([dbPath UTF8String], &database)!=SQLITE_OK) {
sqlite3_close(database);
NSLog(#"Failed to open database");
} else {
char *errorMessage;
NSString *insertSQL = [NSString stringWithFormat:#"INSERT INTO table (table) VALUES (\"%#\")", Bt];
const char *insert_stmt = [insertSQL UTF8String];
if(sqlite3_exec(database, insert_stmt, NULL, NULL, &errorMessage)==SQLITE_OK){
NSLog(#"Data inserted");
[db_status setFloatValue:5];
}
}
}
It would be great if somebody can help me. I think it is just a small error but I don´t get the problem ;(
Best regards,
Robby
by the looks of your IBOUTLET connection, how you have your code, Bt is equal to the TextField. But what you want is the text value in the TextField. so you would use this: NSString *Bt = [NSString stringWithFormat:#"%#", Text.stringValue]; NSTextField has a property named stringValue and this will return the NSString that contains the value inside the textfield, Text.
Allowing Quotation Marks in Your Database
So easiest way, i can think of, is having a category method on NSString. So wherever you're going to save string or text from the user to your database, you would use this function to then insert into your format statement. and example shown below:
Header File of NSString Category
#interface NSString (STRING_)
/**
* Allowing Qoutation marks inside a string to be saved in a SQL column with the string format sourrounded by #"\"%#\"". this will not modify the string, only return a modified copy
* #return NSString *
*/
- (NSString *)reformatForSQLQuries;
#end
Implantation File of NSString Category
#implementation NSString (STRING_)
- (NSString *)reformatForSQLQuries {
NSString *string = self;
//Depending on how you "wrap" your strings to format the string values into your INSERT, UPDATE, or DELETE queries, you'll only have to use one of these statements. YOU DO NOT NEED BOTH
//Use this if you have your format as: '%#'
string = [string stringByReplacingOccurrencesOfString: #"'" withString: #"''"];
//Use this if you have your format as: \"%#\"
string = [string stringByReplacingOccurrencesOfString: #"\"" withString: #"\"\""];
return string;
}
example
...
NSString *insertSQL = [NSString stringWithFormat:#"INSERT INTO table (table) VALUES (\"%#\")", [Bt reformatForSQLQuries]];
...
I got the solution ! It´s false to write:
NSString *Bt = [NSString stringWithFormat:#"%#", Text];
The solution is to get the value from the NSTextField:
NSString *Bt = [NSTextField stringValue];
What I still don´t understand is why it´s not possible to save Strings which include this symbol: "". For example:
The database saves this: I like programming.
But not this: I like "programming".
Can somebody explain me why ?
I am trying to write some simple code that searches two dictionaries for a string and prints to the console if the string appears in both dictionaries. I want the user to be able to input the string via the console, and then pass the string as a variable into a message. I was wondering how I could go about getting a string from the console and using it as the argument in the following method call.
[x rangeOfString:"the string goes here" options:NSCaseInsensitiveSearch];
I am unsure as to how to get the string from the user. Do I use scanf(), or fgets(), into a char and then convert it into a NSSstring, or simply scan into an NSString itself. I am then wondering how to pass that string as an argument. Please help:
Here is the code I have so far. I know it is not succinct, but I just want to get the job done:
#import <Foundation/Foundation.h>
#include <stdio.h>
#include "stdlib.h"
int main(int argc, const char* argv[]){
#autoreleasepool {
char *name[100];
printf("Please enter the name you wish to search for");
scanf("%s", *name);
NSString *name2 = [NSString stringWithFormat:#"%s" , *name];
NSString *nameString = [NSString stringWithContentsOfFile:#"/usr/share/dict/propernames" encoding:NSUTF8StringEncoding error:NULL];
NSString *dictionary = [NSString stringWithContentsOfFile:#"/usr/share/dict/words" encoding:NSUTF8StringEncoding error:NULL];
NSArray *nameString2 = [nameString componentsSeparatedByString:#"\n"];
NSArray *dictionary2 = [dictionary componentsSeparatedByString:#"\n"];
int nsYES = 0;
int dictYES = 0;
for (NSString *n in nameString2) {
NSRange r = [n rangeOfString:name2 options:NSCaseInsensitiveSearch];
if (r.location != NSNotFound){
nsYES = 1;
}
}
for (NSString *x in dictionary2) {
NSRange l = [x rangeOfString:name2 options:NSCaseInsensitiveSearch];
if (l.location != NSNotFound){
dictYES = 1;
}
}
if (dictYES && nsYES){
NSLog(#"glen appears in both dictionaries");
}
}
}
Thanks.
Safely reading from standard input in an interactive manner in C is kind of involved. The standard functions require a fixed-size buffer, which means either some input will be too long (and corrupt your memory!) or you'll have to read in a loop. And unfortunately, Cocoa doesn't offer us a whole lot of help.
For reading standard input entirely (as in, if you're expecting an input file over standard input), there is NSFileHandle, which makes it pretty succinct. But for interactively reading and writing like you want to do here, you pretty much have to go with the linked answer for reading.
Once you have read some input into a C string, you can easily turn it into an NSString with, for example, +[NSString stringWithUTF8String:].
So basically I have an app that will provide tasks based on selected project. Both projects and tasks are stored in a SQLite database.
To get the current project id I compare the selected project (_selectedProject) to my database, to get the ID. This is done in my getSelectedProjectId method. However, when running this method in the getTasks method, the Where-statement wont work at all. If I don't run the getSelectedProjectId method first, it works just fine. Am I forgetting to release something? Or is it something else? Any ideas?
I'm pretty new to both SQLite and Objective C, so this may not be a complex issue. I have made sure the getSelectedProjectId method returns the correct project ID. I have also made sure the query that is run in the getTasks method is correct, and when running it through my terminal it returns a number of rows. In the app it returns nothing, provided I'm running the getSelectedProjectId somewhere in that method first.
This is the method that fetches the tasks:
- (void)getTasks
{
[self openDB];
sqlite3_stmt *statement;
int projectId = [self getSelectedProjectId];
NSString *query = [NSString stringWithFormat:#"SELECT * FROM tasks WHERE project_id=%i", projectId];
const char *query_statement = [query UTF8String];
sqlite3_prepare_v2(_contactDB, query_statement, -1, &statement, NULL);
while (sqlite3_step(statement) == SQLITE_ROW)
{
// I add the task title to my array of tasks here.
}
sqlite3_finalize(statement);
sqlite3_close(_contactDB);
}
And this is the method that gets the correct project id from the database:
- (int)getSelectedProjectId
{
sqlite3_stmt *statement;
NSString *query = [[NSString alloc]
initWithFormat:#"SELECT id FROM projects WHERE title=\"%#\" LIMIT 0,1",
_selectedProject];
int rowId = 0;
const char *query_statement = [query UTF8String];
[self openDB];
sqlite3_prepare_v2(_contactDB, query_statement, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_ROW)
{
rowId = sqlite3_column_int(statement, 0);
}
sqlite3_finalize(statement);
sqlite3_close(_contactDB);
return rowId;
}
The problem occured because I closed the DB connection in my getSelectedProjectId-method. I'm now leaving my DB open instead, works like a charm.
I have this class containing a static variable "database" which represent a database realized with sqlite and a function getAllShop which task is recalling all the data that are stored in the db and filling them into a mutable array
#define kFilename #"negozi.sqlite"
#implementation ShopDatabase
static ShopDatabase *database;
+(ShopDatabase *)database{
if (database==nil) {
database = [[ShopDatabase alloc] init];
return database;
}
}
- (id)init
{
self = [super init];
if (self) {
// Create the path to the database in the Documents directory for the bundle itself is not writable
NSArray *pathsToDocuments = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [pathsToDocuments objectAtIndex:0];
databasePath = [documentsDirectory stringByAppendingPathComponent:kFilename];
if (![[NSFileManager defaultManager] isReadableFileAtPath:databasePath]) {
if ([[NSFileManager defaultManager] copyItemAtPath:yourOriginalDatabasePath toPath:databasePath error:NULL] != YES)
NSAssert2(0, #"Fail to copy database from %# to %#", yourOriginalDatabasePath, databasePath);
}
// --- open db
if(sqlite3_open([databasePath UTF8String], &database) != SQLITE_OK){
NSLog(#"Failed to open database");
}else {
NSLog(#"Database opened");
}
}
return self;
}
- (NSMutableArray *) getAllShops{
// ------ read all the db
NSMutableArray *returnArray=[[NSMutableArray alloc] init];
NSString *query= #"SELECT * FROM negozio";
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, NULL) == SQLITE_OK){
NSLog(#"Prepared database");
while (sqlite3_step(statement)==SQLITE_ROW) {
int uniqueId = sqlite3_column_int(statement, 0);
NSMutableString *nome = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
ShopInfo *info= [[ShopInfo alloc] initWithUniqueId:uniqueId nome:nome];
[returnArray addObject:info];
}
sqlite3_finalize(statement);
}
return returnArray;
}
#end
When i have to take data from the database from another class i do this, calling the getAllShop and all goes well. In this way i have all the data of the db into my array shopinfo:
NSMutableArray *shopInfo=[[ShopDatabase database] getAllShops];
Now, my database contains data that i need to use to fill TWO table view, so i need to execute this TWO times: one time in the class representing the first table view and one in the second. When i do this in the first view all goes well, but when i do the same the second time, Xcode give me a exc bad access error. I tried executing the code two times in the same class and this is what i get
2012-05-11 13:06:54.897 Shopping Mall[11333:707] -[NegozioPartenza getAllShops]: unrecognized selector sent to instance 0x14b8c0
2012-05-11 13:06:54.899 Shopping Mall[11333:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NegozioPartenza getAllShops]: unrecognized selector sent to instance 0x14b8c0'
*** First throw call stack:
(0x33ad188f 0x325c3259 0x33ad4a9b 0x33ad3915 0x33a2e650 0xa4141 0x35727e33 0x3574c629 0x35710d7d 0x357d34dd 0x3571955d 0x3571940b 0x357d34a3 0x35788873 0x357881b7 0x357d1d89 0x357d04eb 0x3582b82b 0x33a2b3fd 0x35709faf 0x35709f6b 0x35709f49 0x35709cb9 0x3570a5f1 0x35708ad3 0x357084c1 0x356ee83d 0x356ee0e3 0x32fa622b 0x33aa5523 0x33aa54c5 0x33aa4313 0x33a274a5 0x33a2736d 0x32fa5439 0x3571ce7d 0xa2515 0xa24c0)
terminate called throwing an exception(lldb)
I am a newbie to objective C so i can't manage to understand what's the point. How can i call two times a function on a static variable? Thank you.
Edit: maybe calling the [ShopDatabase database] activate a second time the constructor of initializazione making mess? When i say that a variable is static it mean there's only one instance of it for every object of that class, right? So how i can access that unique instance after creating it the first time? I think i'm messing up what happen when you use a static variable...
You don't have a valid return on the second call.
+(ShopDatabase *)database{
if (database==nil) {
database = [[ShopDatabase alloc] init];
return database;
}
}
On the second call database is not nil and you don't return anything. You should be getting a warning that not all control paths return a value.
Here is the proper way.
+(ShopDatabase *)database{
if (database==nil) {
database = [[ShopDatabase alloc] init];
}
return database;
}
Somewhere after your first call, database is being released. Subsequently, an object of type NegozioPartenza is being created at the same location. The second call to [ShopDatabase database] returns this object, and you then send -getAllShops to that object, which obviously doesn't implement that method.
I am creating an exercise app that will record the weight used and the number of "reps" the user did in 4 "Sets" per day over a period of 7 days so the user may view their progress.
I have built the database table named FIELDS with 2 columns ROW and FIELD_DATA and I can use the code below to load the data into the db. But the code has a sql statement that says,
INSERT OR REPLACE INTO FIELDS (ROW, FIELD_DATA)VALUES (%d, '%#');
When I change the statment to:
INSERT INTO FIELDS (ROW, FIELD_DATA)VALUES (%d, '%#');
Nothing happens. That is no data is recorded in the db.
Below is the code:
#define kFilname #"StData.sqlite3"
- (NSString *)dataFilePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:kFilname];
}
-(IBAction)saveData:(id)sender;
{
for (int i = 1; i <= 8; i++)
{
NSString *fieldName = [[NSString alloc]initWithFormat:#"field%d", i];
UITextField *field = [self valueForKey:fieldName];
[fieldName release];
NSString *insert = [[NSString alloc] initWithFormat:
#"INSERT OR REPLACE INTO FIELDS (ROW, FIELD_DATA)
VALUES (%d, '%#');",i, field.text];
// sqlite3_stmt *stmt;
char *errorMsg;
if (sqlite3_exec (database, [insert UTF8String],
NULL, NULL, &errorMsg) != SQLITE_OK)
{
// NSAssert1(0, #"Error updating table: %s", errorMsg);
sqlite3_free(errorMsg);
}
}
sqlite3_close(database);
}
So how do I modify the code to do what I want? It seemed like a simple sql statement change at first but obviously there must be more. I am new to Objective-C and iPhone programming.
I am not new to using sql statements as I have been creating web apps in ASP for a number of years.
Any help will be greatly appreciated, this is driving me nuts!
Suggestions:
write an insert statement with hardcoded values to see if the insert works
your filename has no path. does it assume the current directory when executed? what directory is it running from?
write a message to the screen if possible to see what the values you're getting are. Are they correct?