小结Basic GNUstep Base Library Classes 例子

#include <stdio.h>

/* 
 * The next #include line is generally present in all Objective-C
 * source files that use GNUstep.  The Foundation.h header file
 * includes all the other standard header files you need.
 */
#include <Foundation/Foundation.h>  

void
describeArray (NSArray *array)
{
  int i, count;

  count = [array count];
  for (i = 0; i < count; i++)
  {
    NSLog (@"Object at index %d is: %@", 
           i, [array objectAtIndex: i]);
  }
}

void
describeArrayWithEnumerator (NSArray *array)
{
  NSEnumerator *enumerator;
  NSObject *obj;

  enumerator = [array objectEnumerator];

  while ((obj = [enumerator nextObject]) != nil)
    {
      NSLog (@"Next Object is: %@", obj);      
    }
}

void
describeDictionary (NSDictionary *dict)
{ 
  NSArray *keys;
  int i, count;
  id key, value;

  keys = [dict allKeys];
  count = [keys count];
  for (i = 0; i < count; i++)
  {
    key = [keys objectAtIndex: i];
    value = [dict objectForKey: key];
    NSLog (@"Key: %@ for value: %@", key, value);
  }
}

/*
 * The main() function: pass a message to the Test class
 * and print the returned string.
 */
int main(void)
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

#if TRUE
	
	NSString *name = @"Nicola";

	int age = 25;
	NSString *message;
	message = [NSString stringWithFormat: @"Your age is %d", age];
	
	NSString *first;
	NSString *second;
	first = @"Nicola";
	second = [NSString stringWithFormat: @"My name is %@", first];
	
	char *result;
	NSString *string;
	string = @"Hello";
	result = [string UTF8String];
	string = [NSString stringWithUTF8String: result];
	
	NSMutableString *str;
	str = [NSMutableString stringWithFormat: @"Hello, %@", name];
	
	NSString *greeting = @"Hello";
	NSMutableString *s;
	s = AUTORELEASE ([NSMutableString new]);
	[s appendString: greeting];
	[s appendString: @", "];
	[s appendString: name];
	
	/*
	NSString *name = @"This string was created by GNUstep";
	if ([name writeToFile: @"/home/nico/testing" atomically: YES])
	{
	  NSLog (@"Success");
	}
	else 
	{
	  NSLog (@"Failure");
	}
	*/
	
	/*
	NSString *string;
	NSString *filename = @"/home/nico/test";
	string = [NSString stringWithContentsOfFile: filename];
	if (string == nil)
	{
	  NSLog (@"Problem reading file %@", filename);
	  // <missing code: do something to manage the error...>
	  // <exit perhaps ?>
	}
	*/
	
#endif

#if FALSE //TRUE
	
	NSArray *names;
	names = [NSArray arrayWithObjects: @"Nicola", @"Margherita",
                                   @"Luciano", @"Silvia", nil];
  
	/*
	NSArray *objects;
	NSButton *buttonOne;
	NSButton *buttonTwo;
	NSTextField *textField;

	buttonOne = AUTORELEASE ([NSButton new]);
	buttonTwo = AUTORELEASE ([NSButton new]);
	textField = AUTORELEASE ([NSTextField new]);

	objects = [NSArray arrayWithObjects: buttonOne, buttonTwo, 
										 textField, nil];
	*/
  
	NSArray *numbers;
	//NSString *string;
	numbers = [NSArray arrayWithObjects: @"One", @"Two", @"Three", 
									 nil];
	string = [numbers objectAtIndex: 2];   // @"Three"
	
	
	NSArray *array;
	array = [NSArray arrayWithObjects: @"Hi", @"Hello", 
                                   AUTORELEASE ([NSLock new]), 
                                   nil];
	NSLog (@"Array: %@", array);
		
	
	//NSArray *array;
	array = [NSArray arrayWithObjects: @"Nicola", @"Margherita",
                                   @"Luciano", @"Silvia", nil];
	if ([array containsObject: @"Nicola"]) // YES
	{
		// Do something
	}
	
	
	//NSArray *array;
	int i, j;
	array = [NSArray arrayWithObjects: @"Nicola", @"Margherita",
									   @"Luciano", @"Silvia", nil];
	i = [array indexOfObject: @"Margherita"]; // 1
	j = [array indexOfObject: @"Luca"]; // NSNotFound
	
#endif

#if FALSE //TRUE
	
	NSMutableArray *array;
	array = [NSMutableArray new];
	[array addObject: @"Michele"];
	[array addObject: @"Nicola"];
	[array insertObject: @"Alessia" atIndex: 1];
	/* Now the array contains Michele, Alessia, Nicola. */
	
	[array removeObjectAtIndex: 0];
	/* Now the array contains Alessia, Nicola. */
	
	[array replaceObjectAtIndex: 1  withObject: @"Nicola"];
	
	
	NSDictionary *dict;
	NSString *path;
	dict = [NSDictionary dictionaryWithObjectsAndKeys:
               @"/opt/picture.png", @"Luca", 
               @"/home/nico/birthday.png", @"Birthday Photo", 
               @"/home/nico/birthday.png", @"Birthday Image", 
               @"/home/marghe/pic.jpg", @"My Sister", nil];
	// Following will set path to /home/nico/birthday.png
	path = [dict objectForKey: @"Birthday Image"];
	
	// Following will set path to nil
	path = [dict objectForKey: @"My Mother"];
	
	NSString *imageName = @"My Father";
	//NSString *path;
	path = [dict objectForKey: imageName];
	if (path == nil)
	{
		// This means the dictionary does not contain it
		NSLog (@"Don't know the path to the image '%@'", imageName);
	}
	else
	{
		// Do something with path
	}
#endif

#if TRUE //FALSE

	NSMutableDictionary *dict;
	dict = [NSMutableDictionary new];
	AUTORELEASE (dict);
	[dict setObject: @"/opt/picture.png"        
			 forKey: @"Luca"]; 
	[dict setObject: @"/home/nico/birthday.png" 
			 forKey: @"Birthday Photo"];
	[dict setObject: @"/home/nico/birthday.png" 
			 forKey: @"Birthday Image"];
	[dict setObject: @"/home/marghe/pic.jpg"    
			 forKey: @"My Sister"];
		 
	[dict removeObjectforKey: @"Luca"];



	//NSMutableDictionary *dict;
	NSDictionary *dict2;

	dict = [NSMutableDictionary new];
	AUTORELEASE (dict);

	dict2 = [NSDictionary dictionaryWithObjectsAndKeys: 
			 @"Nicola", @"Name",
			 @"Pero", @"Surname",
			 @"[email protected]", @"Email", nil];
	[dict setObject: dict2  forKey: @"Nicola"];

	dict2 = [NSDictionary dictionaryWithObjectsAndKeys: 
			 @"Ettore", @"Name",
			 @"Perazzoli", @"Surname",
			 @"[email protected]", @"Email", nil];
	[dict setObject: dict2  forKey: @"Ettore"];

	dict2 = [NSDictionary dictionaryWithObjectsAndKeys: 
			 @"Richard", @"Name",
			 @"Frith-Macdonald", @"Surname",
			 @"[email protected]", @"Email", nil];
	[dict setObject: dict2  forKey: @"Richard"];

	if ([dict writeToFile: @"/home/nico/testing" atomically: YES])
	{
	  NSLog (@"Success");
	}
	else
	{
	  NSLog (@"Failure");
	}


	//NSDictionary *dict;
	//NSDictionary *dict2;
	NSString *email;

	dict = [NSDictionary dictionaryWithContentsOfFile: 
						 @"/home/nico/testing"];
	dict2 = [dict objectForKey: @"Ettore"];
	email = [dict2 objectForKey: @"Email"];

	NSLog (@"Ettore's Email is: %@", email);	
#endif
	[pool drain];

	return 0;
}


参考:http://www.gnustep.it/nicola/Tutorials/BasicClasses/index.html

你可能感兴趣的:(小结Basic GNUstep Base Library Classes 例子)