【问题标题】:Objective-C: Comparing user input with object's instance variableObjective-C:将用户输入与对象的实例变量进行比较
【发布时间】: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


【解决方案1】:

创建一个 SMADoc 对象数组,而不是您的 authours 数组。 然后,在您的循环中,数组中的每个对象都将有一个 authournumber 您可以匹配。找到合适的对象后,您就可以从同一个对象中挑选出urlToDoc

目前,当您检查数组中的每个对象时,您将其称为SMADoc *,但这是错误的。您创建了一个 NSString * 数组(并且您正确地将其作为字符串进行比较),但您需要的是 real SMADoc *

【讨论】:

  • 感谢您的精彩回答,真的帮了我大忙!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-26
  • 1970-01-01
  • 1970-01-01
  • 2012-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多