【发布时间】:2015-09-22 16:43:38
【问题描述】:
我学习 Objective-C 已经有一段时间了,我决定尝试做一个更大的项目,没有像学习书籍中那样的任何真正的“指南”,但现在我陷入了困境。
我正在尝试通过为这些文档创建可搜索的命令行工具来帮助朋友将他拥有的一些文档数字化。
我想我已经走得很远了,我已经为具有三个变量的文档创建了一个自定义类;作者姓名、文章编号和我电脑上文件的路径(我当然会更改为他将文件存储在他电脑上的位置)。然后,我创建了两个示例文档,其中填充了所有变量。由于文档有两个属性,数字和作者姓名,用户可以搜索这些属性之一。因此,我将用户的输入分隔为字符串或 int(借助堆栈溢出帖子:How to determine if the first character of a NSString is a letter),我还创建了一个包含不同文档的“作者”变量的数组。
这是我遇到的问题:我想遍历“作者”的数组,如果作者的姓名与用户输入的内容匹配,它将打开位于给定路径的文档'UrlToDoc'。问题是,实例变量“UrlToDoc”没有以某种方式“连接”到“leadAuthor”变量(据我所知)。因此,我的问题是,当我在数组中找到与用户所写内容匹配的内容后,如何描述该特定对象的“UrlToDoc”变量? (例如,如果用户输入 jamesson,我如何用值描述 UrlToDoc 变量:/Users/pinkRobot435/Desktop/test1.pdf)
此外,如果用户输入数字,则应使用底部的 else 语句(它会做同样的事情)。虽然我还没有写它,但我猜它的代码在描述“UrlToDoc”变量时几乎是一样的。
这是我的代码:
我的自定义类 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;
}
提前感谢!
【问题讨论】:
-
实际上有一个
NSURL来存储 URL :) -
@ChristianSchnorr 我在谷歌搜索时看到了一点,但我并不真正了解如何使用它,我认为使用 NSString 会更容易。但是感谢您的提示:)
标签: objective-c arrays nsstring command-line-interface