小编lem*_*ist的帖子

如何在目标c中正确地使用SQL注入证明我的查询

我的目标c代码中有一个函数,它使用用户在文本字段中输入的注释更新SQLite表列.我想确保我正确地做到这一点,以便一般没有安全问题或问题.这是我的代码,我可以做些什么来使这更安全,或者它已经可以了吗?

sqlite3_stmt *stmt=nil;
sqlite3 *cruddb;

//insert
const char *sql = "UPDATE Peaks SET notes=? where ID=?";

//Open db
sqlite3_open([path UTF8String], &cruddb);
sqlite3_prepare_v2(cruddb, sql, -1, &stmt, NULL);
sqlite3_bind_text(stmt, 1, [self.viewNotes.text UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_int(stmt, 2, [self.detailItem ID]);

sqlite3_step(stmt);
sqlite3_finalize(stmt);
sqlite3_close(cruddb);
Run Code Online (Sandbox Code Playgroud)

sqlite xcode objective-c xcode4.3

4
推荐指数
1
解决办法
1358
查看次数

if语句比较整数不符合目标c

我试图根据两个整数的值执行逻辑.这里我定义了我的整数,我也有NSLog,所以当我运行代码时,我可以看到值是否正确:

int theHikeFlag = (int)[theNewHikeFlag objectAtIndex:(theID-1)];
NSLog(@"theHikeFlag: %@",theHikeFlag);
int fromTheDB = [self.detailItem hikeFlag];
NSLog(@"fromTheDB: %d",fromTheDB);
Run Code Online (Sandbox Code Playgroud)

这是逻辑:

if (theHikeFlag==1) {
    hikeString=@"You have";
}
else if (theHikeFlag==0) {
    hikeString=@"You have not";
}
else {
    if (fromTheDB==1) {
        hikeString=@"You have";
    }
    else {
        hikeString=@"You have not";
    }
}
Run Code Online (Sandbox Code Playgroud)

作为此代码如何工作的示例.当theHikeFlag=1fromTheDB=0,代码绕过if和else if并直接进入else并设置hikeString="You have not".这意味着我的结果与theHikeFlag无关,并且基于fromTheDB整数.

xcode objective-c xcode4.3

2
推荐指数
1
解决办法
3322
查看次数

当我在其中搜索NSString时,如何确定NSMutableArray中对象的索引?

目前,我正在使用来自表格中显示的数据库的数据来编写UISearchBar的搜索功能.我找出搜索结果并将其保存到NSMutableArray *searchResults.这是看起来如何:

- (void) searchGrapesTableView {
    [searchResult removeAllObjects];
    numSearchWines=0;
    for (NSString *str in listOfWines)
    {
        NSRange titleResultsRange = [str rangeOfString:searchBar.text options:NSCaseInsensitiveSearch];
        if (titleResultsRange.length > 0) {
            [searchResult addObject:str];
            numSearchWines++;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

listOfWines也是一个NSMutableArray,它包含可以从中搜索的所有名称的列表.理想情况下,我想确定listOfWines具有匹配值的对象的索引,并将其添加到另一个NSMutableArray,这样我以后可以很容易地访问它.例如,稍后当我使用此搜索数据显示表格单元格时,我有以下代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"wineCell"];
    //determine how many to add to get the grape ID
    NSInteger grapeID=0;
    for (int i=0; i<indexPath.section; i++) {
        grapeID += [self.tableView numberOfRowsInSection:i];
    }

    Wines *currentWine;
    if (isSearchOn) {
        NSString *cellValue …
Run Code Online (Sandbox Code Playgroud)

xcode objective-c nsmutablearray uisearchbar xcode4.3

1
推荐指数
1
解决办法
130
查看次数