gpt4 book ai didi

iphone - stanford cs193p - PrintIntrospectionInfo - 第 4 节作业 1b

转载 作者:行者123 更新时间:2023-12-03 17:33:10 26 4
gpt4 key购买 nike

我在 stanford iPhone 类(class)第 4 部分的作业 1b 上遇到了问题。

我无法理解如何构建数组以及什么作业预期。

数组应该是“全局”变量吗?我应该在哪里定义它?其他每个子函数都会将其变量添加到数组中吗?PrintIntrospectionInfo 函数只是用来枚举的吗并打印所有内省(introspection)信息?

我在哪里可以阅读他们在本文中要求的内容任务?我应该读什么?

我不参加类(class),所以有人可以向我发送他们的代码吗?这经验教训是相互积累的,我希望能够继续下去。

这是作业:

Objective-C has a number of facilities that add to its dynamic object-oriented capabilities.  Many of these facilities deal with determining and using an object's capabilities at runtime.  Create a mutable array and add objects of various types to it. Create instance of the classes we’ve used elsewhere in this assignment to populate the array: NSString, NSURL, NSProcessInfo, NSDictionary, etc. Create some NSMutableString instances and put them in the array as well.
Feel free to create other kinds of objects also.
Iterate through the objects in the array and do the following: 1. Print the class name of the object. 2. Log if the object is member of class NSString. 3. Log if the object is kind of class NSString. 4. Log if the object responds to the selector "lowercaseString". Page 5 of 6 5. If the object does respond to the lowercaseString selector, log the result of asking the object to perform that selector (using performSelector:) CS193P Assignment 1B Spring 2009 Doll/Cannistraro

最佳答案

您可以在 Xcode 附带的 cocoa 和 Objective-C 手册中找到答案。

#import <Foundation/Foundation.h>

void printIntrospectionInfo()
{
NSMutableArray * array = [NSMutableArray arrayWithCapacity:5];
[array addObject: [NSString stringWithString:@"Example NSString object"]];
[array addObject: [NSMutableString stringWithString:@"Example NSMutableString object"]];
[array addObject: [NSURL URLWithString:@"http://apple.com.au"]];
[array addObject: [NSProcessInfo processInfo]];
[array addObject: [NSDictionary dictionaryWithObject: @"DictObject" forKey: @"KeyObject"]];
[array addObject: [NSNumber numberWithInt:123456]];

SEL sel_lowercase = @selector(lowercaseString);

int i;
for (i = 0; i < [array count]; ++i)
{
id o = [array objectAtIndex:i];

NSLog(@"%@", o);
NSLog(@"Class name: %@", [[o class] className]);
NSLog(@"Is Member of NSString: %@", ([o isMemberOfClass: [NSString class]] ? @"YES" : @"NO"));
NSLog(@"Is Kind of NSString: %@", ([o isKindOfClass: [NSString class]] ? @"YES" : @"NO"));
NSLog(@"Responds to lowercaseString: %@", ([o respondsToSelector: sel_lowercase] ? @"YES" : @"NO"));

if ([o respondsToSelector: sel_lowercase])
NSLog(@"lowercaseString: %@", [o performSelector: sel_lowercase]);

NSLog(@"===================");
}

}


int main(int argc, const char* argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
printIntrospectionInfo();
[pool release];
return 0;
}

关于iphone - stanford cs193p - PrintIntrospectionInfo - 第 4 节作业 1b,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1447518/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com