简体   繁体   English

有关在Objective-C中创建类的问题

[英]Question about creating classes in Objective-C

I am really new to objective C, and I want to make a class that is an NSArray of NSDictionary, and then have a method that grabs a random entries. 我对目标C真的很陌生,我想创建一个NSDictionary的NSArray类,然后提供一个捕获随机条目的方法。 I know how to make that but I don't understand how to make it in the class. 我知道如何做到这一点,但我不理解如何在课堂上做到这一点。 What I mean is I thought that you could put the code that declared (or whatever the correct terminology is) the array just sort of in the middle of the implementation file and then I would write a method under that. 我的意思是,我认为您可以将声明了该数组(或任何正确的术语)的代码放在实现文件的中间,然后在此之下编写一个方法。 The only instance variable I had was the NSArray and that was in the interface file, along with the method prototype (or whatever) and these were the only things that were in the interface file. 我拥有的唯一实例变量是NSArray,它位于接口文件中,以及方法原型(或其他方法),而这些是接口文件中唯一的东西。

I couldn't figure out the problem so I made a test class that was the same but with just an array of simple text strings. 我无法弄清问题,所以我做了一个测试类,但是只有一个简单的文本字符串数组。 I used the same logic here and I'm pretty certain it is totally backward, I don't know in which way though. 我在这里使用了相同的逻辑,但我可以肯定它是完全落后的,但是我不知道以哪种方式。

This is the interface for the test class: 这是测试类的接口:

#import <Foundation/Foundation.h>


@interface TestClass : NSObject {
    NSArray *TestArray;
}


@end

And this is the implementation file 这是实现文件

#import "TestClass.h"


@implementation TestClass{
    NSArray *TestArray;
}
TestArray = [[NSArray alloc] arrayWithObjects:@"stuff",@"things",@"example",@"stuffThings",nil];

@end

You should really read Apple's introduction to Objective-C . 您应该真正阅读Apple对Objective-C的介绍 It explains the syntax and structure of the language. 它解释了该语言的语法和结构。 You must also read the Objective-C memory management guide so that your programs don't leak memory and crash. 您还必须阅读《 Objective-C内存管理指南》,以确保程序不会泄漏内存并导致崩溃。

Having said that, here's probably what you're trying to create (I took the liberty of changing some of your variable names): 话虽如此,这可能是您要尝试创建的(我自由更改了一些变量名):

TestClass.h

#import <Foundation/Foundation.h>

@interface TestClass : NSObject {
  NSArray* strings_;
}

// Method declarations would go here
// example:
- (NSString*)randomElement;

@end

TestClass.m

#import "TestClass.h"
#import <stdlib.h>

// Notice how the implementation does NOT redefine the instance variables.
@implementation TestClass

// All code must be in a method definition.

// init is analogous to the default constructor in other languages
- (id)init {
  if (self = [super init]) {
    strings_ = [[NSArray alloc] initWithObjects:@"stuff", @"things", nil];
  }
  return self;
}

// dealloc is the destructor (note the call to super).
- (void)dealloc {
  [strings_ release];
  [super dealloc];
}

- (NSString*)randomElement {
  NSUInteger index = arc4random() % [strings_ count];
  return [strings_ objectAtIndex:index];
}

@end

For random number generation , it's easy to use arc4random() because it doesn't require setting the seed value. 对于随机数生成 ,使用arc4random()很容易,因为它不需要设置种子值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM