I know that print line is NSLog. How do I print a line 10 times in Objective-C?
I haven't had a chance to refactor yet, but something like this should work!
for(int i = 0; i < 10; i++)
{
if(i == 0)
NSLog(#"A line");
else if (i == 1)
NSLog(#"A line");
else if (i == 2)
NSLog(#"A line");
else if (i == 3)
NSLog(#"A line");
else if (i == 4)
NSLog(#"A line");
else if (i == 5)
NSLog(#"A line");
else if (i == 6)
NSLog(#"A line");
else if (i == 7)
NSLog(#"A line");
else if (i == 8)
NSLog(#"A line");
else if (i == 9)
NSLog(#"A line");
}
Another while loop option.
//
// main.m
// Pritner
//
// Created by Joshua Caswell on 7/19/12.
//
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
// A Pritner instance holds and displays a passed-in string. The string is publicly
// unchangeable.
#interface Pritner : NSObject
+ (id)pritnerWithLine: (NSString *)newLine;
- (void)printLine;
#property (readonly, copy, nonatomic) NSString * line;
#end
// Extension to manage "class variable" for counting number of created instances
#interface Pritner ()
+ (NSUInteger)numPritnersCreated;
+ (void)setNumPritnersCreated:(NSUInteger)n;
+ (NSUInteger)maxNumPritners;
#property (readwrite, copy, nonatomic) NSString * line;
- (id)initWithLine: (NSString *)line;
#end
#implementation Pritner
#synthesize line;
+ (id)pritnerWithLine: (NSString *)newLine {
id newInstance = [[self alloc] initWithLine:newLine];
if( newInstance ){
NSUInteger createdSoFar = [self numPritnersCreated];
// Only allow maxNumPritners to ever be created; keeping track of them
// is the client's problem.
if( createdSoFar >= [self maxNumPritners] ){
abort();
}
[self setNumPritnersCreated:createdSoFar + 1];
}
return newInstance;
}
// Fake class variable using associated objects; keep count of created instances
char numPritnerKey;
+ (NSUInteger)numPritnersCreated {
NSNumber * n = objc_getAssociatedObject(self, &numPritnerKey);
if( !n ){
n = [NSNumber numberWithUnsignedInteger:0];
[self setNumPritnersCreated:0];
}
return [n unsignedIntegerValue];
}
+ (void)setNumPritnersCreated:(NSUInteger)n {
objc_setAssociatedObject(self,
&numPritnerKey,
[NSNumber numberWithUnsignedInteger:n],
OBJC_ASSOCIATION_RETAIN);
}
// Maximum number of instances ever allowed to be created
+ (NSUInteger)maxNumPritners {
return 10;
}
- (id)initWithLine: (NSString *)newLine {
self = [super init];
if( !self ) return nil;
line = [newLine copy];
return self;
}
- (void)printLine {
NSLog(#"%#", [self line]);
}
#end
int main(int argc, const char * argv[])
{
#autoreleasepool {
while( YES ){
Pritner * p = [Pritner pritnerWithLine:#"I figure, if you're going to build a time machine out of a car, why not do it with some style?"];
[p printLine];
}
}
return 0;
}
Please don't use this in real life.
Don't forget the "object" in "Objective-C".
NSArray *lines = [#"123456789" componentsSeparatedByCharactersInSet:[NSCharacterSet alphanumericCharacterSet]];
[lines enumerateObjectsUsingBlock:^(id obj, NSUInteger i, BOOL* stop) {
NSLog(#"a line");
}];
If you think Objective-C isn't true enough to its Smalltalk roots, you can do something like the following.
typedef void (^LoopBlock)(NSNumber*);
#interface NSNumber (loop)
-(void)to:(NSNumber*)upTo do:(LoopBlock)block;
-(void)timesRepeat:(LoopBlock)block;
#end
#implementation NSNumber (loop)
static NSString *loopSeparator = #"_";
-(void)to:(NSNumber*)upTo do:(LoopBlock)block {
[ [ [#"" stringByPaddingToLength:[upTo unsignedIntegerValue]-[self unsignedIntegerValue]
withString:loopSeparator
startingAtIndex:0
] componentsSeparatedByString:loopSeparator
] enumerateObjectsUsingBlock:^(id obj, NSUInteger i, BOOL* stop) {
block([NSNumber numberWithUnsignedInteger:i+[self unsignedIntegerValue]]);
}
];
}
-(void)timesRepeat:(LoopBlock)block {
[[NSNumber numberWithUnsignedInteger: 1] to:self do:block];
}
#end
int main() {
#autoreleasepool {
[[NSNumber numberWithInt:10]
timesRepeat:^(NSNumber* i){
NSLog(#"a line");
}];
}
}
Use recursion:
print(10);
void print(int i){
if(i == 0)
return;
NSLog(#"print this line");
print(i - 1);
}
Use Grand Central Dispatch another way:
dispatch_apply(10, dispatch_get_main_queue(), ^(size_t curr_iteration){
NSLog(#"a line");
});
Use a goto statement:
int i = 0;
print:
NSLog(#"print this line");
if (i++ < 10) goto print;
Use a for loop:
for (int i = 0; i < 10; i++) {
NSLog(#"print this line");
}
Use Grand Central Dispatch:
__block int i = 0;
__block dispatch_block_t print_block = ^() {
NSLog(#"print this line");
i += 1;
if (i < 10) dispatch_sync(dispatch_get_main_queue(), print_block);
}
dispatch_sync(dispatch_get_main_queue(), print_block);
Use a while loop:
int i = 0;
while (i < 10) {
NSLog(#"print this line");
i += 1;
}
Related
I have TextField with value binded to Document's serverAddress property (readwrite, copy), and TextField formatter delegate connected with ServerAddressFormatter object in XIB.
It's actually working with input like 127.0.0.1:8080, but as soon as I put something without : TextField clear itself completely.
Here's ServerAddressFormatter implementation:
#implementation ServerAddressFormatter
- (NSString *) stringForObjectValue:(NSArray *)obj {
if ([obj isKindOfClass:[NSArray class]]) {
return [obj componentsJoinedByString:#":"];
} else {
return #"";
}
}
- (BOOL)getObjectValue:(out id *)anObject
forString:(NSString *)string
errorDescription:(out NSString **)error {
int i;
for (i = (int) ([string length] == 0 ? 0 : [string length] - 1); i > 0; i--) {
if ([string characterAtIndex:i] == ':') break;
}
if (i == 0) {
*anObject = #[string]; // if I put string, #"100" here it's working fine
} else {
*anObject = #[[string substringToIndex:i], [string substringFromIndex:i+1]];
}
return YES;
}
#end
I want to mimic NSDirectoryEnumerator with a simple recursive function, but my attempts so far have had my loops stopping prematurely.
The method needs to stop when there is no longer a directory or file present at an nth level.
How can I rearrange the method below to be a simple recursive function that will terminate accordingly?
Any help would be much appreciated. Thanks.
#import "MenuFromPathData.h"
#interface MenuFromPathData ()
#property (nonatomic,strong) NSFileManager *myFileManager;
#end
#implementation MenuFromPathData
#synthesize myFileManager=_myFileManager;
#define ROOT_DIRECTORY #"TextData"
//
////
//
- (void) simpleMenuArrayBuilder
{
NSArray *fileNamesLevel1 = [self fullArrayReturn:ROOT_DIRECTORY];
for(int i = 0; i < [fileNamesLevel1 count]; i++)
{
NSString *pathNameLevel2 = [NSString stringWithFormat:#"%#/%#",ROOT_DIRECTORY,[fileNamesLevel1 objectAtIndex:i]];
NSArray *fileNamesLevel2 = [self fullArrayReturn:pathNameLevel2];
for(int j = 0; j < [fileNamesLevel2 count]; j++)
{
NSString *pathNameLevel3 = [NSString stringWithFormat:#"%#/%#/%#",ROOT_DIRECTORY,[fileNamesLevel1 objectAtIndex:i],[fileNamesLevel2 objectAtIndex:j] ];
NSArray *fileNamesLevel3 = [self fullArrayReturn:pathNameLevel3];
for(int k = 0; k < [fileNamesLevel3 count]; k++)
{
NSString *pathNameLevel4 = [NSString stringWithFormat:#"%#/%#/%#/%#",ROOT_DIRECTORY,[fileNamesLevel1 objectAtIndex:i],[fileNamesLevel2 objectAtIndex:j],[fileNamesLevel3 objectAtIndex:k] ];
NSArray *fileNamesLevel4 = [self fullArrayReturn:pathNameLevel4];
for(int l = 0; l < [fileNamesLevel4 count]; l++)
{
NSString *pathNameLevel5 = [NSString stringWithFormat:#"%#/%#/%#/%#/%#",ROOT_DIRECTORY,[fileNamesLevel1 objectAtIndex:i],[fileNamesLevel2 objectAtIndex:j],[fileNamesLevel3 objectAtIndex:k],[fileNamesLevel4 objectAtIndex:l] ];
NSArray *fileNamesLevel5 = [self fullArrayReturn:pathNameLevel5];
for(int m = 0; m < [fileNamesLevel5 count]; m++)
{
NSString *pathNameLevel6 = [NSString stringWithFormat:#"%#/%#/%#/%#/%#/%#",ROOT_DIRECTORY,[fileNamesLevel1 objectAtIndex:i],[fileNamesLevel2 objectAtIndex:j],[fileNamesLevel3 objectAtIndex:k],[fileNamesLevel4 objectAtIndex:l], [fileNamesLevel5 objectAtIndex:m] ];
NSArray *fileNamesLevel6 = [self fullArrayReturn:pathNameLevel6];
NSLog(#"-- LVL 6.0 %# -- ",pathNameLevel6);
for(int n = 0; n < [fileNamesLevel6 count]; n++)
{
NSString *pathNameLevel7 = [NSString stringWithFormat:#"%#/%#/%#/%#/%#/%#%#",ROOT_DIRECTORY,[fileNamesLevel1 objectAtIndex:i],[fileNamesLevel2 objectAtIndex:j],[fileNamesLevel3 objectAtIndex:k],[fileNamesLevel4 objectAtIndex:l], [fileNamesLevel5 objectAtIndex:m],[fileNamesLevel6 objectAtIndex:n] ];
NSArray *fileNamesLevel7 = [self fullArrayReturn:pathNameLevel7];
NSLog(#"-- LVL 7.0 %# -- ",pathNameLevel7);
}
}
}
}
}
}
}
//
////
//
- (NSString*) resourcePath : (NSString*) pathName {
return [[NSBundle mainBundle] pathForResource:pathName ofType:nil];
}
- (NSArray*) bundleArrayReturn : (NSString*)pathForResource {
NSError* error;
return [self.myFileManager contentsOfDirectoryAtPath:pathForResource error:&error];
}
- (NSArray*) fullArrayReturn : (NSString*) pathName {
return [self bundleArrayReturn:[self resourcePath: pathName]];
}
//
////
//
- (NSFileManager*) myFileManager {
if(!_myFileManager) {
_myFileManager = [NSFileManager defaultManager];
}
return _myFileManager;
}
#end
Something like this should work. Only trick is to add another parameter to your method to track current depth during the recursion.
- (void) simpleMenuArrayBuilderForPath:(NSString*)pathToDecend allowedDepth:(int)depth
{
// if we've defended far enough, then stop
if(depth == 0) return;
// otherwise, get the next directory listing at this level
NSArray *fileNamesNextLevel = [self fullArrayReturn:pathToDecend];
for(int i = 0; i < [fileNamesNextLevel count]; i++)
{
// find each path inside the directory we're looking at
NSString *nextLevelPathName = [pathToDecend stringByAppendingPathComponent:[fileNamesNextLevel objectAtIndex:i]];
// process it however you want...
NSLog(#"Looking at %#", nextLevelPathName);
// and recur into it
[self simpleMenuArrayBuilderForPath:nextLevelPathName allowedDepth:depth-1];
}
}
then, call it by:
[self simpleMenuArrayBuilderForPath:ROOT_DIRECTORY allowedDepth:5];
Coming from C++, here's my question :
I have created objects of this type :
Size *one = [[Size alloc] initWithX: 3 andY: 1];
Size *two = [[Size alloc] initWithX: 4 andY: 7];
// etc...
Size *thirtythree = [[Size alloc] initWithX: 5 andY: 9];
( with a #property int x; & #property int y; for each object.. )
that I have stored in an array as follows :
NSArray *arrayOfSizes;
arrayOfSizes = [NSArray arrayWithObjects:one,two,three,four,five,six,
seven,eight,nine,ten,eleven,twelve,thirteen,
fourteen,fifteen,sixteen,seventeen,eighteen,
nineteen,twenty,twentyone,twentytwo,
twentythree,twentyfour,twentyfive,twentysix,
twentyseven,twentyeight,twentynine,thirty,
thirtyone,thirtytwo,thirtythree nil];
now I have a single object of type :
Myobject *myObject = [[Myobject alloc] initWithX: 5 andY: 3];
that also has a #property int x; & #property int y; ...
and I want to compare its values to the values of the objects found in the array, until I find an array object of similar values.. But I don't know how to do that in Obj-C. (in c++ I would simply use a vector v; with v.size(); and v[x]; ..etc... I suppose..)
here's what I'm looking for.. :)
while( !wholeOfArrayOfSizesChecked && !found)
{
if ( // x & y of object in array is equal to x & y of myObject )
{
found = YES;
}
else if( // whole of array checked)
{
wholeOfArrayOfSizesChecked = YES;
}
else
{
//move on to the next object of the array..
}
}
Thanks in advance for any help!
Well, you could just use fast enumeration on the array. Something like this:
Myobject *myObject = [[Myobject alloc] initWithX: 5 andY: 3];
for (Size *s in arrayOfSizes)
{
if (s.x == myObject.x && s.y == myObject.y)
{
// Found one
// Do something useful...
break;
}
}
Another one:
NSUInteger index = [arrayOfSizes indexOfObjectPassingTest:
^BOOL(Size *s, NSUInteger idx, BOOL *stop)
{
return (s.x == myObject.x) && (s.y == myObject.y);
}
];
if (index != NSNotFound) {
id object = [arrayOfSizes objectAtIndex:index];
}
Just to use your given structure. There are smarter ways of doing it though :)
wholeOfArrayOfSizesChecked = NO;
int currObj = 0
while( !wholeOfArrayOfSizesChecked && !found)
{
Size *current = (Size *)[arrayOfSizes objectAtIndex:i];
if (myObject.x == current.x && myObject.y == current.y)
{
found = YES;
}
else if(currObj == [arrayOfSizes count] -1 )
{
wholeOfArrayOfSizesChecked = YES;
}
else
{
currObj++;
}
}
Try something like this:
for (int i = 0; i < [arrayOfSizes size]; i++)
{
Size *current = (Size *)[arrayOfSizes objectAtIndex:i];
if (myObject.x == current.x && myObject.y == current.y)
{
// found
break;
}
}
How'bout a for-in loop?
for (Size *item in array) {
// compare 'item' to myObject
if (/* equal condition here */) break;
}
-(BOOL) isSize:(Size*)size equalToMyObject:(MyObject*)object{
return (size.x == object.x) && (size.y == object.y);
}
//In some method where you are checking it:
for (Size* size in arrayOfSizes){
if ([self isSize:size equalToMyObject:myObject]){
//You found it! They're equal!
break;
}
}
I'm taking a class and we're working on a Calculator program. My background is in C++. I am taking a RPN calculator entry of 3 enter sqrt and need to display it as sqrt(3) in my descriptionOfProgram method, which is new, including associated property below. Here's the class so far. Search for "xcode" to find my issues. Any ideas? I'm not very good at the basic objective c classes, but I'm trying to learn. Here's a summary:
it's complaining about my boolean. I'm not sure why. I did this in a different class and it worked fine.
it's looking for a { I don't see it
it doesn't like my use of the key. I'm unclear on how to get the key's contents I think is the problem.
It wants ] but I'm not seeing why
skipped
It expected } at #end
Hope you can help! Thanks!
//
// CalculatorBrain.m
// Calculator
//
// Created by Michele Cleary on 2/25/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "CalculatorBrain.h"
#interface CalculatorBrain()
#property (nonatomic, strong) NSMutableArray *programStack;
#property (nonatomic, strong) NSDictionary *testVariable;
#property (nonatomic) BOOL numberHandledNextOperation;
- (double) convertRadianToDegree: (double) radian;
#end
#implementation CalculatorBrain
#synthesize programStack = _programStack;
#synthesize testVariable = _testVariable;
#synthesize numberHandledNextOperation = _numberHandledNextOperation;
- (NSMutableArray *)programStack
{
if (_programStack == nil) _programStack = [[NSMutableArray alloc] init];
return _programStack;
}
//- (void)setOperandStack:(NSMutableArray *)operandStack
//{
// _operandStack = operandStack;
//}
- (void)pushOperand:(double)operand
{
[self.programStack addObject:[NSNumber numberWithDouble:operand]];
}
- (double)performOperation:(NSString *)operation
{
[self.programStack addObject:operation];
return[CalculatorBrain runProgram:self.program];
}
- (id)program
{
return [self.programStack copy];
}
+ (NSString *)descriptionOfProgram:(id)program
{
self.numberHandledNextOperation = NO; //1. this is a problem with xcode: member reference type struct objc_class * is a pointer; maybe you meant to use ->
NSMutableSet * displayDescrip = [[NSMutableSet alloc] init];
for(id foundItemKey in program)
{
if ([foundItemKey isKindOfClass:[NSString class]])
//operator or variable
{
if ([foundItemKey isEqualToString:#"sin"]&&(!self.numberHandledNextOperation))
{ //2. xcode says To match this {.
NSObject *nextObj = [program objectForKey:(foundItemKey+1); //3. xcode doesn't like this: arithmetic on pointer to interface id which is not a constant size in non-fragile ABI
//[displayDescrip addObject:foundItemKey];
}
else if ([foundItemKey isEqualToString:#"cos"])
{
//[displayDescrip addObject:foundItemKey];
}
else if ([foundItemKey isEqualToString:#"sqrt"])
{
//[displayDescrip addObject:foundItemKey];
}
else if ([foundItemKey isEqualToString:#"Ï€"])
{
//[displayDescrip addObject:foundItemKey];
}
else if (![CalculatorBrain isOperationName:foundItemKey])
{
//variable
//[displayDescrip addObject:foundItemkey];
}
else if (foundItemKey isKindOfClass:[NSNumber class]) //4. xcode expected ]
{
//number
//if next object is operation
if(isOperation([program objectForKey:(foundItemKey+1)))
{
numberHandledNextOperation = YES;
if(isOperationSpecial([program objectForKey:(foundItemKey+1)))
{ //sin or cos or sqrt need parentheses
//[displayDescrip addObject:(foundItemKey+1)];
//[displayDescrip addObject:#"("];
//[displayDescrip addObject:foundItemKey];
//[displayDescrip addObject:#")"];
}
else
{ //regular operation + - / *
//[displayDescrip addObject:(foundItemKey+1)];
//[displayDescrip addObject:(foundItemKey)];
}
numberHandledNextOperation = YES;
} //if
} //else if
} //if
} //for
//not sure if I need this next thing
//NSSet * returnedVarNames = [varNames copy];
//return returnedVarNames;
return #"implement this in Assignment 2";
}
+ (double)runProgram:(id)program
{
NSMutableArray *stack;
if ([program isKindOfClass:[NSArray class]]) {
stack = [program mutableCopy];
}
return [self popOperandOffStack:stack];
}
+ (double)runProgram:(id)program usingVariableValues:(NSDictionary *)variableValues
{
NSMutableArray *stack;
if ([program isKindOfClass:[NSArray class]]) {
stack = [program mutableCopy];
}
if(variableValues)
{
int numItemsDisplayed = [stack count];
for (int count = 0; count < numItemsDisplayed; count++)
{
id foundItem = [stack objectAtIndex:count];
if ([foundItem isKindOfClass:[NSString class]])
{
NSString * var = [variableValues objectForKey:foundItem];
if(var)
{
[stack replaceObjectAtIndex:count withObject:[NSNumber numberWithDouble:[var doubleValue]]];
}
}
}
}
return [self popOperandOffStack:stack];
}
+ (double)popOperandOffStack:(NSMutableArray *)stack
{
double result = 0;
id topOfStack = [stack lastObject];
if (topOfStack) [stack removeLastObject];
if([topOfStack isKindOfClass:[NSNumber class]]){ //number
result = [topOfStack doubleValue];
}
else if ([topOfStack isKindOfClass:[NSString class]]){ //string operation
NSString *operation = topOfStack;
if ([operation isEqualToString:#"+"]) {
result = [self popOperandOffStack:stack] + [self popOperandOffStack:stack];
}else if ([operation isEqualToString:#"*"]) {
result = [self popOperandOffStack:stack] * [self popOperandOffStack:stack];
}else if ([operation isEqualToString:#"/"]) {
double divisor = [self popOperandOffStack:stack];
if (divisor)
result = [self popOperandOffStack:stack] / divisor;
}else if ([operation isEqualToString:#"-"]) {
double subtrahend = [self popOperandOffStack:stack];
result = [self popOperandOffStack:stack] - subtrahend;
}else if ([operation isEqualToString:#"sin"]) {
result = result = (sin([self popOperandOffStack:stack])); //(sin([self convertRadianToDegree:[self popOperandOffStack:stack]]));
}else if ([operation isEqualToString:#"cos"]) {
result = (cos([self popOperandOffStack:stack]));
}else if ([operation isEqualToString:#"sqrt"]) {
result = (sqrt([self popOperandOffStack:stack]));
}else if ([operation isEqualToString:#"π"]) {
result = M_PI;
}else{
result = 0;
}
}
return result;
}
+ (NSSet *)variablesUsedInProgram:(id)program
{
NSMutableSet * varNames = [[NSMutableSet alloc] init];
for(id foundItem in program)
{
if ([foundItem isKindOfClass:[NSString class]])
{
if (![CalculatorBrain isOperationName:foundItem])
{
[varNames addObject:foundItem];
}
}
}
NSSet * returnedVarNames = [varNames copy];
return returnedVarNames;
}
+ (BOOL)isOperationName:(NSString *)foundItem
{
NSSet *myOperationSet = [NSSet setWithObjects:#"sqrt", #"sin", #"cos", #"π", #"+", #"-", #"*", #"/", nil];
return([myOperationSet containsObject:(foundItem)]);
}
- (NSString *)description
{
return [NSString stringWithFormat:#"stack = %#", self.programStack];
}
-(double) convertRadianToDegree: (double) radian;
{
return M_PI*2*radian/360;
}
#end //6. xcode expected }
+ (NSString *)descriptionOfProgram:(id)program
Do you actually want descriptionOfProgram a class + method ? If yes, it is more like a static method in C++. It doesn't belong to any particular instance of a class. There is no hidden parameter of constant pointer to the current instance is passed.
I'm in the process of converting AMShellWrapper to my own application that runs an SH file that has userinput. Therefore, I need to send data to a running task.
Any ideas?
Elijah
You need a somewhat different approach along the lines of PseudoTTY.app!
/*
code added to PseudoTTY/PtyView.m
sources:
- PseudoTTY.app, http://amath.colorado.edu/pub/mac/programs/PseudoTTY.zip
- charliebot/server.sh, http://sourceforge.net/projects/charliebot/
(note: modify server.sh to accept complete paths; see:
http://stackoverflow.com/questions/3540269/noclassdeffounderror-when-running-shell-script)
*/
#interface PtyView (PtyPrivate)
-(int)count: (NSString *) str;
...
#end
#implementation PtyView (PtyPrivate)
-(int)count: (NSString *) str {
static int counter = 0;
if (
([str rangeOfString:#"Charlie>"].location != NSNotFound ) || \
([str rangeOfString:#"[Charlie] user>"].location != NSNotFound )
)
{
counter++;
}
return counter;
}
-(void)startTask
{
NSString * cmd = #"/path/to/charliebot/server.sh";
//NSString * cmd = #"/bin/sh";
...
[self insertText: #"\n\n"];
}
-(void) didRead: (NSNotification *)noty
{
NSData * data = [[noty userInfo] objectForKey:NSFileHandleNotificationDataItem];
if ([data length] == 0)
return; // end of file
NSString * str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
int printvar = [self count: str];
if (printvar < 1 )
{
[self insertText: #"."];
[str release];
[[noty object] readInBackgroundAndNotify];
}else if (printvar == 1) {
[self insertText: #"\n\n"];
[self insertText: str];
[str release];
[[noty object] readInBackgroundAndNotify];
}else {
[self insertText: str];
[str release];
[[noty object] readInBackgroundAndNotify];
}
}
#end