我在我的 CRHViewControllerScript 类中创建了一个名为“bulletedArray”的 NSArray,我试图访问同一个数组并将其写在我的 CRHCommentSection 类的 NSLog 中。但是,它不是在写数组。我知道数组有效,可以在第一堂课上写出来。我认为我上下一堂课的方法很糟糕,但这是我的代码的样子。
“CRHViewControllerScript.h”
@interface CRHViewControllerScript : UIViewController <UITextViewDelegate> {
__weak IBOutlet UITextView *myTextView;
}
+ (NSArray*)theArray;
- (IBAction)chalkYes:(id)sender;
@end
“CRHViewControllerScript.m”
#import "CRHViewControllerScript.h"
static NSArray* bulletedArray = nil;
@interface CRHViewControllerScript ()
@end
@implementation CRHViewControllerScript
+ (NSArray*)theArray {
return bulletedArray;
}
- (IBAction)chalkYes:(id)sender {
//get the text inside the textView
NSString *textContents = myTextView.text;
textContents = [textContents stringByReplacingOccurrencesOfString:@"\n" withString:@""];
//make the array
NSArray *bulletedArray = [textContents componentsSeparatedByString:@"\u2022"];
//eliminate blank component at top of the array ("")
NSUInteger len = bulletedArray.count;
if (bulletedArray.count) {
bulletedArray = [bulletedArray subarrayWithRange:NSMakeRange(1, len-1)];
}
//print out array
NSLog(@"%@",bulletedArray);
}
CRHCommentSection.h
#import <UIKit/UIKit.h>
@interface CRHCommentSection : UIViewController
@end
CRHCommentSection.m
#import "CRHCommentSection.h"
#import "CRHViewControllerScript.h"
@interface CRHCommentSection ()
@end
@implementation CRHCommentSection
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSArray *myArray = [CRHViewControllerScript theArray]; // This is how to call a Class Method in Objective C
NSLog(@"%@", myArray);
}