Objective-C的:与对象的实例变量比较用户输入 [英] Objective-C: Comparing user input with object's instance variable

查看:134
本文介绍了Objective-C的:与对象的实例变量比较用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在学习Objective-C一会儿,我决定尝试,并采取稍微大一点的项目没有任何真正的指引作为学习的书籍,但我现在已经卡住了。

I have been learning Objective-C for a while, and I decided to try and take on a little bigger project without any real "guidelines" as in the learning books but now I have got stuck.

我试图做的是帮朋友digitalising几个文件,他有,通过创建这些文件可搜索的命令行工具。

What I'm trying to do is helping a friend digitalising a few documents he have, by creating a searchable commandline-tool for these documents.

我想我已经得到了pretty为止,我已经创建了文件的自定义类有三个变量;作者姓名,文章的数量和文件路径在我的电脑(我当然会改变,他有自己的存储在计算机上的文件的地方)。我已然后创建与所有填充在变量两个示例文档。由于文档具有两个属性,数字及作者的名字,用户可以搜索这些属性中的一个。因此,我分开了用户的输入是字符串或int(在堆栈溢出后的帮助:<一href=\"http://stackoverflow.com/questions/16860108/how-to-determine-if-the-first-character-of-a-nsstring-is-a-letter\">How确定一个NSString的的第一个字符是字母)我还创建了一个阵列的不同文件的'author'可变的。

I think I have gotten pretty far, I have created a custom class for documents with three variable; name of the author, number of the article and the path to the file on my computer (which I of course will change to the place he have the documents stored on his computer). I have then created two example documents with all the variables filled in. Since the documents have two properties, numbers and name of the author, the user may search for one of these properties. I therefore separated the input of the user to either be a string or a int (with help of a stack overflow post: How to determine if the first character of a NSString is a letter ) I also created an array with the 'author'-variable's of the different documents.

这是被我击中凹凸:我想通过作者的阵列来运行,如果作者的名字匹配用户什么都投入了,它会打开它是在给定的路径文件UrlToDoc。问题是,在某种方式实例变量UrlToDoc不是连接到leadAuthor'变量(据我可以告诉)。因此,我的问题是,我如何,我都与用户所写的内容在数组中找到了匹配后,形容UrlToDoc'变量为特定的对象? (如果用户在输入jamesson,例如,如何描述与价值UrlToDoc变量:/Users/pinkRobot435/Desktop/test1.pdf)

This is were I have hit a bump: I want to run through the array of the 'author' and if the author's name match with what the user have put in, it will open the document which is at the path given at 'UrlToDoc'. The problem is, the instance variable 'UrlToDoc' is not "connected" to the 'leadAuthor'-variable in some kind of way (as far as I can tell). My question is therefore, how do I, after I have found a match in in the array with what the user written, describe the 'UrlToDoc'-variable for that specific object? (If the user typed in jamesson, for instance, how do I describe the UrlToDoc variable with the value: /Users/pinkRobot435/Desktop/test1.pdf )

此外,如果用户在一个写入次数,应使用在底部的else语句(这将做同样的事情)。我没有写它尚未虽然,但我猜code表示这将是pretty大同小异,描述了UrlToDoc'变量时。

Also, if the user writes in a number, the else-statement on the bottom (which would do the same thing) should be used. I haven't written it yet though, but I guess the code for it would be pretty much the same, when describing the 'UrlToDoc'-variable.

下面是我的code:

我的自定义类SMADoc:

My custom 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

SMADoc.m

#import "SMADoc.h"

@implementation SMADoc

@end

main.m文件

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;
}

谢谢AVANCE!

Thanks in avance!

推荐答案

而不是你的 authours 达阵,创造 SMADoc 的对象。
然后,在你的循环,从阵列中的每个对象将有一个 authour 数量可以匹配。当右找到一个,你可以随便挑了 urlToDoc 出同一对象的。

Instead of your authours array, create an array of SMADoc objects. Then, in your loop, each object from the array will have an authour or number you can match. When the right one is found, you can just pick the urlToDoc out of the same object.

目前,我们正在调用数组中的每个对象 SMADoc * 当你审视它,但这是错误的。你创建的NSString * 的数组(和你比较它是正确的字符串),但你需要有什么样的一个真正 SMADoc *

Currently, you're calling each object in the array a SMADoc * when you examine it, but that's wrong. You created an array of NSString * (and you're comparing it as a string correctly) but what you need there is a real SMADoc *.

这篇关于Objective-C的:与对象的实例变量比较用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆