Page 2 of 2
<  1  2
Reply
   
 chrome's cocoa coding calisthenics! exercise 1 
 
 
  #16 (permalink)  
Old 04-04-2008, 11:35 PM
NSLog(@"%@", [self customTitle]);

Group: Regulars
Location: Melbourne


Can someone do this with bindings in an array controller or something in such a way that they can write even less code?
__________________
"A company must go out and find what the customer wants ... The need is not for, say, half a million ¼-inch drill bits. The needs is that there are ten million ¼-inch holes that need to be drilled." - Robert Noyce

"Algorithms that forget their history are doomed to repeat it" - Artificial Intelligence, A Modern Approach (Russel & Norvig)
forgie is offline
Profile CardPM
Go to the top of the page
Digg this Post!Add Post to del.icio.us Share on Facebook
Reply With Quote
  #17 (permalink)  
Old 04-04-2008, 11:54 PM
Regular

Group: Regulars
Location: S37°56.2' E145°07.6'


Quote:
Originally Posted by forgie View Post
Can someone do this with bindings in an array controller or something in such a way that they can write even less code?
I like your thinking!

Only two of the seven lines of code needed were actually hand written. The rest was generated by Interface Builder.

One of the two lines simply loaded the Cocoa Ruby bindings.

The remaining line of code is the one that actually does all the work... Appending strings should be a hard task.

Hooray for Ruby!

I wonder how many lines of code would be needed for the Python version. Any Python programmers care to give it a try?
toholio is offline
Profile CardPM
Go to the top of the page
Digg this Post!Add Post to del.icio.us Share on Facebook
Reply With Quote
  #18 (permalink)  
Old 11-04-2008, 11:04 AM
MacTalk Engineering Dept.

Group: Regulars
Location: Sydney, Australia


I suspect that you'll need to write code to

a) Append text to the bottom of the text field and scroll it into view

b) Erase the text in the text entry field

Thats all the code I have. I guess you could make an array controller, and have the output text view changed to a list view, and then adding an entry/scrolling to the bottom would be easy, but selecting text wouldn't work as expected.

My implementation:

ExampleController.h:
Code:
#import <Cocoa/Cocoa.h>

@interface ExampleController : NSObject {
	IBOutlet NSTextView		*textOutput;
	IBOutlet NSTextField	*textInput;
}

- (IBAction) button_add:(id)sender;

- (void) appendString:(NSString *)str;

@end
ExampleController.m:
Code:
#import "ExampleController.h"


@implementation ExampleController

- (IBAction) button_add:(id)sender
{
	NSLog(@"event: button_add: %@", sender);
	
	[self appendString:textInput.stringValue];
	[self appendString:@"\n"];
	[textInput setStringValue:@""];
}

- (void) appendString:(NSString *)str;
{
	NSRange endRange;
	
	endRange.location = [[textOutput textStorage] length];
	endRange.length = 0;
	[textOutput replaceCharactersInRange:endRange withString:str];
	endRange.length = [str length];
	[textOutput scrollRangeToVisible:endRange];		
}

@end
Thanks everyone who had a crack at this, I'll post up the next exercise today
__________________
Mac Pro 2 x 4 core 2.8Ghz 16GB | Mac Book Pro 17" | iPhone 3G 16GB | 20" iMac 2 Core 2 Duo
Are you an Internode user? Download NodeQuota 1.0.7 - an Internode Usage Meter for Leopard!
chrome is offline
Profile CardPM
Go to the top of the page
Digg this Post!Add Post to del.icio.us Share on Facebook
Reply With Quote
  #19 (permalink)  
Old 11-04-2008, 05:32 PM
Regular

Group: Regulars
Location: S37°56.2' E145°07.6'


My text box was an NSTextField so it didn't have scrolling and whatnot. The complete code for my controller was the following.
Code:
require 'osx/cocoa'

class InsertController < OSX::NSObject
  ib_outlets :textBox, :textField

  ib_action :insert do |sender|
    @textBox.stringValue += @textField.stringValue + "\n"
  end
end
If I did want to use an NSTextView with scrolling the code would have to look like this:
Code:
require 'osx/cocoa'

class InsertController < OSX::NSObject
  ib_outlets :textBox, :textView

  ib_action :insert do |sender|
    @textView.string += @textBox.stringValue + "\n"
    @textView.scrollRangeToVisible( OSX::NSRange.new( @textView.string.length, 0 ) )
  end
end
You'll note in the second version I've renamed the outlets and "swapped" them. I don't think my original version was clear.

So, is anyone more interested in Ruby-Cocoa after seeing this solution?
toholio is offline
Profile CardPM
Go to the top of the page
Digg this Post!Add Post to del.icio.us Share on Facebook
Reply With Quote
  #20 (permalink)  
Old 11-04-2008, 07:30 PM
MacTalk Engineering Dept.

Group: Regulars
Location: Sydney, Australia


To be honest, for scripted Cocoa, I think Nu is a lot more promising than either Python or Ruby, as it's syntax is much closer to Objective-C's, so the library access is almost identical.

Programming Nu
chrome is offline
Profile CardPM
Go to the top of the page
Digg this Post!Add Post to del.icio.us Share on Facebook
Reply With Quote
  #21 (permalink)  
Old 12-04-2008, 07:36 AM
Regular

Group: Regulars


thanks heaps, ill start this afternoon
__________________
There's only one thing that gets me fired up, the voice of every person that said I couldn't make it.
Muzza_77 is online now
Profile CardPM
Go to the top of the page
Digg this Post!Add Post to del.icio.us Share on Facebook
Reply With Quote
 
Page 2 of 2
<  1  2
Reply

Thread Tools

 
Similar Threads
 
Thread Thread Starter Forum Replies Last Post
Coding. Where to start? Lachie Terminal, UNIX, Command line & X11 32 07-04-2008 12:27 AM
Email coding vid Mac OS X & All Software 8 19-10-2005 11:53 AM
html coding Jimbo Projects: Audio, Graphics, Video, HTPC and Programming 22 18-02-2005 07:38 AM