-3

作为强制性验证,我通过此方法检查我的文本字段是否为空:

- (NSString *) clean: (NSString *) str
{
    return [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

并在插入数据库时​​:

if([self clean:textfield.text].length == 0)
{
//Error
}

我想知道我是否必须使用 <= 0 ?以防止以后出现某种可疑的错误/错误。就像我们在网站中一样(SQL 注入和其他)。有人可以在 iOS APP 的文本字段中插入负长度字符串吗?

4

2 回答 2

2
NSSTring *text = [textfield.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

 if ([text length] == 0 )
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Please    enter text." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

    [alert show];

}

IT will check whether the textfield is empty or not !!!!!!!

于 2013-04-19T11:39:47.117 回答
2

According to apple documentation, the prototype of length method is :

- (NSUInteger)length

Handle the negative values is then not necessary (you have an unsigned return value), so " == 0" is ok for testing.

于 2013-04-19T11:46:40.040 回答