The goal of this lab is for you to create a "chat bot" that can perform some useful commands.
You will do this by modifying a premade project, subclassing a simplistic bot to create a more complicated one.
To do this lab, you'll need to download the prebuilt ChatBot framework. Open it in Xcode to get started.
XXChatBot
(with your initials instead of XX, of course).XXChatBot
from NSObject
to CBChatBot
, the class of the simple bot which is currently installed.#import "CBChatBot.h"
at the top of the header file, since you are subclassing CBChatBot
.CBChatController
to use your custom bot. To do this, first open CBChatController.m. The file uses CBChatBot
in two places, #import "CBChatBot.h"
and bot = [[CBChatBot alloc] init];
. Replace CBChatBot
with your bot's class name.CBChatBot
. If there are any errors, check to see that you have done the above steps correctly. You can see any errors by clicking on the red icon in the bottom right of the Xcode window.[XXChatBot screenName]
; in order to change the screen name, you must redefine the + (NSString *)screenName
method. Note that it is a class method, which is why it begins with a plus sign (the screen name cannot depend on any internal object state).+ (NSString *)screenName
which simply return
s the name you've chosen as an NSString *
. You can see an example implementation in CBChatBot.m
. Remember, to build an NSString
, put your @"text in a pair of double quotes preceded by an at sign"
.
- (void)respondToChatMessage:(NSString *)chatMessage
method in your subclass. Add a definition for this method in your implementation file.sendMessage:
, which is defined in the superclass, CBChatBot
. Pass the argument @"hello"
(so that it says hello). This method call will look something like [self sendMessage:@"hello"]
.
@"hello"
. The text you sent is passed into the method as the string chatMessage
. To see whether two objects are equal, use - (BOOL)isEqual:(id)other
(if you're interested, here's why you can't just use ==). Use this method with an if
statement (if (condition) { code }
) to execute your earlier code only when the chatMessage
is @"hello"
.+ (NSDate *)date
is the method to get the current date and time. It is a class method, so you must send it to the NSDate
class directly.- (NSString *)description
method.respondToChatMessage:
method. In your XXChatBot.h file, add an instance variable of type NSString *
called rememberedString
.NSString
to do this is called - (BOOL)hasPrefix:(NSString *)prefixString
.rememberedString
variable, you must call retain
on the message string. Otherwise Objective-C may deallocate it before you recall it later. Note that this leaks memory, but we won't worry about that now. Your code will look something like this: rememberedString = [chatMessage retain];
(if you're curious about retain
and feel like reading ahead, check out the Memory Management Programming Guide for Cocoa).NSTimer
is the Cocoa timer class; take a minute to browse its documentation.+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)target selector:(SEL)selector userInfo:(id)userInfo repeats:(BOOL)yesOrNo
. You should read the documentation about using timers to see how to use it.scheduledTimerWithTimeInterval:(NSTimeInterval)ti
— the first argument is an NSTimeInterval
, a time duration in seconds.target:(id)target
— when the timer fires, it sends a message to the target
specified here.
selector:(SEL)selector
— and this is the message it sends. This pattern of target and selector appears everywhere in Cocoa. To specify a method name, use the syntax @selector(nameOfMethod:includingArguments:)which gives you a value of type
SEL
.userInfo:(id)userInfo
— sometimes, especially if you have more than one timer, it's useful to pass extra data around. When the timer calls the selector
on its target
, it will pass your userInfo
data along. In this lab, though, you should just pass in nil
.repeats:(BOOL)yesOrNo
— if you pass YES
, the timer will keep firing at the time interval you specified. If you pass NO
it will fire only once. Pretty straightforward.NSString
method - (NSString *)substringFromIndex:(NSUInteger)index
is what you want here (NSUInteger
just means a positive number).NSString
containing the digits of a number — what you really wanted was a time interval. Fortunately, [string floatValue]
will return the floating-point numeric value of such a string (NSTimeInterval
is a C floating-point number type).- (void)timerTriggered:(NSTimer *)timer
in your XXChatBot class for the timer to call. When called, it should output, "ding!"timerTriggered:
is a method defined on the chat bot object, the target
for the timer method will be self
.selector
will be the timerTriggered:
method — in code, this looks like @selector(timerTriggered:)
.NO
for repeats
.[NSTimer scheduledTimerWithTimeInterval:___ target:___ selector:___ userInfo:nil repeats:___]
.hasPrefix:
method on NSString
, just like before.