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

发布于 2024-08-05 08:39:37 字数 811 浏览 2 评论 0原文

我在 stanford iphone 课程第 4 部分的作业 1b 上遇到了问题。

我无法理解如何构建数组以及什么 任务期望。

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

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

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

这是作业:

Objective-C 有许多工具可以增强其动态面向对象的功能。许多 这些工具处理在运行时确定和使用对象的功能。 创建一个可变数组并向其中添加各种类型的对象。创建类的实例 我们在此分配的其他地方使用了以下内容来填充数组:NSString、NSURL、NSProcessInfo、 NSDictionary等。创建一些NSMutableString实例并将它们也放入数组中。
也可以随意创建其他类型的对象。
迭代数组中的对象并执行以下操作: 1. 打印对象的类名。 2. 记录该对象是否是 NSString 类的成员。 3. 记录该对象是否属于 NSString 类。 4. 记录对象是否响应选择器“lowercaseString”。 第 5 页(共 6 页) 5. 如果对象确实响应 lowercaseString 选择器,则记录询问的结果 对象来执行该选择器(使用performSelector:) CS193P 作业 1B 2009 年春季娃娃/Cannistraro

I'm having trouble with section 4 of the stanford iphone class on assignment 1b.

I am having trouble understanding how I will build the array and what
the assignment expects.

Should the array be a "global" variable? Where should I define that?
Will each of the other subfunctions add their variables to the array?
Is the PrintIntrospectionInfo function just there to enumerate through
and print all introspective info?

Where can I read up on the things they are asking for in this
assignment? What should I read up on?

I'm not taking the class so can someone send me their code? The
lessons build up on each other and I want to be able to proceed.

Here's the assignment :

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

☆獨立☆ 2024-08-12 08:39:37

您可以在 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;
}

You can find your answers in cocoa and objective-c manuals that come with Xcode.

#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;
}
廻憶裏菂餘溫 2024-08-12 08:39:37

第一个作业与其说是关于设计,不如说是关于 Objective-C 的感受。全局变量很好,但局部变量会容易得多。

The first assignment wasn't so much about design as it was about feeling around Objective-C. A global variable is fine, but a local variable would be much easier.

彼岸花ソ最美的依靠 2024-08-12 08:39:37

我在哪里可以阅读他们在这项作业中要求的内容?我应该读什么?

除了本地与全局问题(本地更好)之外,请阅读 NSObject 文档。所有四个问题都与您将使用 NSObjects 实现的方法执行的操作有关。

不要忘记阅读 Objective-C

Where can I read up on the things they are asking for in this assignment? What should I read up on?

Aside from the local-vs.-global issue (locals are better), read the NSObject docs. All four questions pertain to things you'll do with methods implemented by NSObjects.

Don't forget to also read up on Objective-C.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文