Objective-C: Comparing user input with object's instance variable

断了今生、忘了曾经 提交于 2019-12-13 04:32:07

问题


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.

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.

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.

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 )

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.

Here is my code:

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

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

Thanks in avance!


回答1:


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.

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 *.



来源:https://stackoverflow.com/questions/31230551/objective-c-comparing-user-input-with-objects-instance-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!