principleMinor
Compare words in one file
Viewed 0 times
onecomparefilewords
Problem
I have started learning programming in ObjC from
"Objective-C Programming: The Big Nerd Ranch Guide"
In a task to exercise the learned stuff I've had to write a program what compares the the names in the file /usr/share/dict/words if there are same proper names (capitalized) and normal words (uncapitalized).
This is my program:
```
#import
int main(int argc, const char * argv[])
{
@autoreleasepool {
// Zähler für die Anzahl der Suchergebnisse
int counter = 0;
// Hier die Suchbegriffe eingeben
NSString *search1 = @"Da";
NSString *search2 = @"d";
// Prüfung ob search1 und search2 identisch sind, wenn nicht eine Warnmeldung ausgeben.
if ([search1 caseInsensitiveCompare:search2] != NSOrderedSame) {
NSLog(@"Wenn du unterschiedliche Wörter suchst, wird die Suche lange dauern und zu keinem Ergebnis führen.");
NSLog(@"Suche ist gestartet...");
sleep(2);
} else {NSLog(@"Suche wird gestartet...");
sleep(2);
// Liest Datei als großen String ein (mögliche Fehler werden ausgeblendet
NSString *namestring = [NSString stringWithContentsOfFile:@"/usr/share/dict/words"
encoding:NSUncachedRead
error:NULL];
// Zerlegt Datei in ein Array mit Strings
NSArray *names = [namestring componentsSeparatedByString:@"\n"];
// Suchalgorithmus
// Geht das Array in der 1. Dimension einen String nach dem anderen durch
for (NSString *n in names) {
// Sucht nach dem String search1
NSRange r = [n rangeOfString:search1 options:NSAnchoredSearch];
// Wurde für search1 etwas gefunden?
if (r.location != NSNotFound) {
// Geht das Array in der 2. Dimension einen String nach dem anderen durch
for (NSString *m in names) {
// Sucht nach dem String search2
NSRange q = [m rangeOfString:search2 options:NSAnchoredS
"Objective-C Programming: The Big Nerd Ranch Guide"
In a task to exercise the learned stuff I've had to write a program what compares the the names in the file /usr/share/dict/words if there are same proper names (capitalized) and normal words (uncapitalized).
This is my program:
```
#import
int main(int argc, const char * argv[])
{
@autoreleasepool {
// Zähler für die Anzahl der Suchergebnisse
int counter = 0;
// Hier die Suchbegriffe eingeben
NSString *search1 = @"Da";
NSString *search2 = @"d";
// Prüfung ob search1 und search2 identisch sind, wenn nicht eine Warnmeldung ausgeben.
if ([search1 caseInsensitiveCompare:search2] != NSOrderedSame) {
NSLog(@"Wenn du unterschiedliche Wörter suchst, wird die Suche lange dauern und zu keinem Ergebnis führen.");
NSLog(@"Suche ist gestartet...");
sleep(2);
} else {NSLog(@"Suche wird gestartet...");
sleep(2);
// Liest Datei als großen String ein (mögliche Fehler werden ausgeblendet
NSString *namestring = [NSString stringWithContentsOfFile:@"/usr/share/dict/words"
encoding:NSUncachedRead
error:NULL];
// Zerlegt Datei in ein Array mit Strings
NSArray *names = [namestring componentsSeparatedByString:@"\n"];
// Suchalgorithmus
// Geht das Array in der 1. Dimension einen String nach dem anderen durch
for (NSString *n in names) {
// Sucht nach dem String search1
NSRange r = [n rangeOfString:search1 options:NSAnchoredSearch];
// Wurde für search1 etwas gefunden?
if (r.location != NSNotFound) {
// Geht das Array in der 2. Dimension einen String nach dem anderen durch
for (NSString *m in names) {
// Sucht nach dem String search2
NSRange q = [m rangeOfString:search2 options:NSAnchoredS
Solution
The first most glaring problem I see is
And a call to
There are other ways to delay actions in our code without putting the entire thread to sleep. In the case of this simple code, there's no interaction anyways, but in the future, you will run into this problem.
For the most simple example of how to point you in the right direction, Objective-C offers
But there's also a non-OOP way of executing code after delays in Objective-C (it's a lot messier than
A code block is similar to a C-style function, but we have a variable that is a pointer to it (yes, I know C and other languages have "function pointers"--we call them blocks in Objective-C). Anyway, the syntax for executing code in a block after a delay looks like this:
Where
sleep(2). We really don't want to be doing this, particularly with Objective-C programs. The main reason I add emphasis on Objective-C is because virtually every program written in Objective-C is either an OSX or iOS app with a GUI. And in these cases, the main thread is the thread that handles the GUI. You'll also be writing the bulk of your code on the main thread--certainly until you start figuring out multithreading. Everything defaults on the main thread.And a call to
sleep(someNum) on the main thread means that the user only has one way of interacting with our program for someNum amount of time--by closing it out.There are other ways to delay actions in our code without putting the entire thread to sleep. In the case of this simple code, there's no interaction anyways, but in the future, you will run into this problem.
For the most simple example of how to point you in the right direction, Objective-C offers
performSelector:withObject:afterDelay: (though this requires the use of objects & methods). But there's also a non-OOP way of executing code after delays in Objective-C (it's a lot messier than
performSelector:withObject:afterDelay:, and I had to look up the exact syntax...). It makes use of code blocks.A code block is similar to a C-style function, but we have a variable that is a pointer to it (yes, I know C and other languages have "function pointers"--we call them blocks in Objective-C). Anyway, the syntax for executing code in a block after a delay looks like this:
int delay = dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC);
dispatch_after(delay, dispatch_get_main_queue(), ^{
// any code you want executed after delay
// in this case, delay is 2 seconds
});Where
2 is the number of seconds you want to delay, and everything else is built-in C or Objective-C constants/functions.Code Snippets
int delay = dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC);
dispatch_after(delay, dispatch_get_main_queue(), ^{
// any code you want executed after delay
// in this case, delay is 2 seconds
});Context
StackExchange Code Review Q#47100, answer score: 5
Revisions (0)
No revisions yet.