Objective-C: 将用户输入与对象的实例变量进行比较
Objective-C: Comparing user input with object's instance variable
我已经学习 Objective-C 一段时间了,我决定尝试做一个更大的项目,而不像学习书籍中那样真正 "guidelines" 但现在我被卡住了。
我想做的是通过为这些文档创建一个可搜索的命令行工具,帮助一位朋友将他拥有的一些文档数字化。
我想我已经走得很远了,我已经为具有三个变量的文档创建了自定义 class;作者姓名、文章编号和我计算机上文件的路径(我当然会更改为他在计算机上存储文件的位置)。然后我创建了两个示例文档,其中填充了所有变量。由于文档有两个属性,编号和作者姓名,因此用户可以搜索这些属性之一。因此,我将用户的输入分为字符串或整数(借助堆栈溢出 post: How to determine if the first character of a NSString is a letter )我还创建了一个数组,其中包含 'author'-变量不同的文档。
这是我遇到的一个问题:我想 运行 通过 'author' 的数组,如果作者的名字与用户输入的内容匹配,它将打开'UrlToDoc' 给出的路径中的文档。问题是,实例变量 'UrlToDoc' 在某种程度上不是 "connected" 到 'leadAuthor' 变量(据我所知)。因此,我的问题是,在数组中找到与用户编写的内容匹配后,如何描述该特定对象的 'UrlToDoc' 变量? (例如,如果用户输入 jamesson,我如何使用以下值描述 UrlToDoc 变量:/Users/pinkRobot435/Desktop/test1.pdf )
此外,如果用户输入数字,则应使用底部的 else 语句(其作用相同)。虽然我还没有写它,但我猜它的代码在描述 'UrlToDoc'-变量时几乎是一样的。
这是我的代码:
我的自定义 class SMADoc:
SMADoc.h
#import <Foundation/Foundation.h>
@interface SMADoc : NSObject
//Two strings, and a pathway to the documnt, with the purpose of describing the document
@property (nonatomic) int number;
@property (nonatomic) NSString *authour;
@property (nonatomic) NSString *urlToDoc;
@end
SMADoc.m
#import "SMADoc.h"
@implementation SMADoc
@end
main.m
#import <Foundation/Foundation.h>
#import "SMADoc.h"
#include <readline/readline.h>
#include <stdlib.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
SMADoc *one = [[SMADoc alloc] init];
[one setnumber:123];
[one setauthour:@"jamesson"];
[one setUrlToDoc:@"/Users/pinkRobot435/Desktop/test1.pdf"];
SMADoc *two = [[SMADoc alloc] init];
[two setnumber:124];
[two setauthour:@"marc"];
[two setUrlToDoc:@"/Users/pinkRobot435/Desktop/test2.pdf"];
NSMutableArray *authours = [[NSMutableArray alloc] initWithObjects: [one authour], [two authour], nil];
NSLog(@"Enter what you want to search for: ");
const char *searchC = readline(NULL);
NSString *searchOrg = [NSString stringWithUTF8String:searchC];
NSString *search = [searchOrg lowercaseString];
NSRange first = [search rangeOfComposedCharacterSequenceAtIndex:0];
NSRange match = [search rangeOfCharacterFromSet:[NSCharacterSet letterCharacterSet] options:0 range:first];
if (match.location != NSNotFound) {
//The string starts with a letter and the array of authour names should be searched through
for (SMADoc *nSearch in authours) {
if ([search isEqualToString:nSearch]) {
**//Open the file that is represented by UrlToDoc for that specific object**
} else {
NSLog(@"The authour was not found, please try again");
}
}
} else {
//The string starts with a number and should be converted to an int and then the array of numbers (which I have not yet created) should be searched through
int number = atoi(searchC);
}
}
return 0;
}
提前致谢!
创建一个 SMADoc
对象数组,而不是 authours
数组。
然后,在您的循环中,数组中的每个对象都会有一个可以匹配的 authour
或 number
。当找到合适的对象时,您可以从同一对象中挑出 urlToDoc
个。
目前,您在检查数组中的每个对象时都将其称为 SMADoc *
,但这是错误的。您创建了一个 NSString *
的数组(并且您将其正确地作为字符串进行比较)但是您需要的是 real SMADoc *
.
我已经学习 Objective-C 一段时间了,我决定尝试做一个更大的项目,而不像学习书籍中那样真正 "guidelines" 但现在我被卡住了。
我想做的是通过为这些文档创建一个可搜索的命令行工具,帮助一位朋友将他拥有的一些文档数字化。
我想我已经走得很远了,我已经为具有三个变量的文档创建了自定义 class;作者姓名、文章编号和我计算机上文件的路径(我当然会更改为他在计算机上存储文件的位置)。然后我创建了两个示例文档,其中填充了所有变量。由于文档有两个属性,编号和作者姓名,因此用户可以搜索这些属性之一。因此,我将用户的输入分为字符串或整数(借助堆栈溢出 post: How to determine if the first character of a NSString is a letter )我还创建了一个数组,其中包含 'author'-变量不同的文档。
这是我遇到的一个问题:我想 运行 通过 'author' 的数组,如果作者的名字与用户输入的内容匹配,它将打开'UrlToDoc' 给出的路径中的文档。问题是,实例变量 'UrlToDoc' 在某种程度上不是 "connected" 到 'leadAuthor' 变量(据我所知)。因此,我的问题是,在数组中找到与用户编写的内容匹配后,如何描述该特定对象的 'UrlToDoc' 变量? (例如,如果用户输入 jamesson,我如何使用以下值描述 UrlToDoc 变量:/Users/pinkRobot435/Desktop/test1.pdf )
此外,如果用户输入数字,则应使用底部的 else 语句(其作用相同)。虽然我还没有写它,但我猜它的代码在描述 'UrlToDoc'-变量时几乎是一样的。
这是我的代码:
我的自定义 class SMADoc:
SMADoc.h
#import <Foundation/Foundation.h>
@interface SMADoc : NSObject
//Two strings, and a pathway to the documnt, with the purpose of describing the document
@property (nonatomic) int number;
@property (nonatomic) NSString *authour;
@property (nonatomic) NSString *urlToDoc;
@end
SMADoc.m
#import "SMADoc.h"
@implementation SMADoc
@end
main.m
#import <Foundation/Foundation.h>
#import "SMADoc.h"
#include <readline/readline.h>
#include <stdlib.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
SMADoc *one = [[SMADoc alloc] init];
[one setnumber:123];
[one setauthour:@"jamesson"];
[one setUrlToDoc:@"/Users/pinkRobot435/Desktop/test1.pdf"];
SMADoc *two = [[SMADoc alloc] init];
[two setnumber:124];
[two setauthour:@"marc"];
[two setUrlToDoc:@"/Users/pinkRobot435/Desktop/test2.pdf"];
NSMutableArray *authours = [[NSMutableArray alloc] initWithObjects: [one authour], [two authour], nil];
NSLog(@"Enter what you want to search for: ");
const char *searchC = readline(NULL);
NSString *searchOrg = [NSString stringWithUTF8String:searchC];
NSString *search = [searchOrg lowercaseString];
NSRange first = [search rangeOfComposedCharacterSequenceAtIndex:0];
NSRange match = [search rangeOfCharacterFromSet:[NSCharacterSet letterCharacterSet] options:0 range:first];
if (match.location != NSNotFound) {
//The string starts with a letter and the array of authour names should be searched through
for (SMADoc *nSearch in authours) {
if ([search isEqualToString:nSearch]) {
**//Open the file that is represented by UrlToDoc for that specific object**
} else {
NSLog(@"The authour was not found, please try again");
}
}
} else {
//The string starts with a number and should be converted to an int and then the array of numbers (which I have not yet created) should be searched through
int number = atoi(searchC);
}
}
return 0;
}
提前致谢!
创建一个 SMADoc
对象数组,而不是 authours
数组。
然后,在您的循环中,数组中的每个对象都会有一个可以匹配的 authour
或 number
。当找到合适的对象时,您可以从同一对象中挑出 urlToDoc
个。
目前,您在检查数组中的每个对象时都将其称为 SMADoc *
,但这是错误的。您创建了一个 NSString *
的数组(并且您将其正确地作为字符串进行比较)但是您需要的是 real SMADoc *
.