0

我允许用户从我的网站复制注册密钥,然后将 NSStirng 保存到剪贴板,当应用程序通过注册对话框打开时,我立即使用此代码检查剪贴板 -

NSString *registrationCode = [UIPasteboard generalPasteboard].string;

现在我有了粘贴板中的值,我想检查它的格式。它应该类似于AAAA-AAAA-AAAA-AAAA所以本质上没有数字,没有空格(包括前面和结尾),没有小写,没有特殊字符...只有大写字符。我怎么能做到这一点?

4

1 回答 1

0

尝试在您的代码中执行此操作:

NSCharacterSet * everythingThatsAllowedSet = [NSCharacterSet characterSetWithCharactersInString: @"ABCDEFGHIJKLMNOPQRSTUVWXYZ-"];
NSCharacterSet * characterSetFromRegistrationCode = [NSCharacterSet characterSetWithCharactersInString: registrationCode];

if([everythingThatsAllowedSet isSupersetOfSet: characterSetFromRegistrationCode ] == YES)
    NSLog( @"this is a valid registration code");
else
{
    // if there are any characters in the registration code that aren't members of
    // the everythingAllowed set, the "isSupersetOfSet" test will fail
    NSLog( @"this has invalid characters");
}
于 2013-08-15T22:00:17.513 回答