This function is not executed well.
-(void)sampleItemA:(NSString*)a itemB:(NSString*)b itemC:(NSDate*)c{
NSLog(#"A");
NSArray* ary = [[NSArray alloc] initWithObjects:a, b, c, nil];
NSLog([ary description]);
NSLog(#"B");
}
log
[Session started at 2009-11-07 20:46:10 +0900.]
2009-11-07 20:46:19.170 xxx[2374:207] A
What is the cause?
EDIT:
I tried.
But it not executed.
-(void)sampleItemA:(NSString*)a itemB:(NSString*)b itemC:(NSDate*)c{
NSLog(#"A");
NSArray* ary = [[NSArray alloc] initWithObjects:a, b, c, nil];
NSLog(#"%#", [ary description]);
NSLog(#"B");
}
log
[Session started at 2009-11-07 21:25:37 +0900.]
2009-11-07 21:25:48.738 xxx[2455:207] A
It is generally unwise to pass nonconstant format strings into NSLog, things get wonky. Try:
-(void)sampleItemA:(NSString*)a itemB:(NSString*)b itemC:(NSDate*)c{
NSLog(#"A");
NSArray* ary = [[NSArray alloc] initWithObjects:a, b, c, nil];
NSLog(#"%#", [ary description]);
NSLog(#"B");
}
I change NSArray to NSMutableArray.
It is executed.
-(void)sampleItemA:(NSString*)a itemB:(NSString*)b itemC:(NSDate*)c{
NSLog(#"A");
NSArray* ary = [[NSMutableArray alloc] init];
[ary addObject:a];
[ary addObject:b];
[ary addObject:c];
NSLog(#"%#", [ary description]);
NSLog(#"B");
}
Related
I am trying to keep my application in dock, however I am not able to do this.
How I am trying to do?
#interface UserDefaultsHelper : NSUserDefaults
- (BOOL)addApplicationToDock:(NSString *)path;
- (BOOL)removeApplicationFromDock:(NSString *)name;
#end
#implementation UserDefaultsHelper
- (BOOL)addApplicationToDock:(NSString *)path {
NSDictionary *domain = [self persistentDomainForName:#"com.apple.dock"];
NSArray *apps = [domain objectForKey:#"persistent-apps"];
NSArray *matchingApps = [apps filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"%K CONTAINS %#", #"tile-data.file-data._CFURLString", path]];
if ([matchingApps count] == 0) {
NSMutableDictionary *newDomain = [domain mutableCopy];
NSMutableArray *newApps = [apps mutableCopy];
NSDictionary *app = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObjectsAndKeys:path, #"_CFURLString", [NSNumber numberWithInt:0], #"_CFURLStringType", nil] forKey:#"file-data"] forKey:#"tile-data"];
[newApps addObject:app];
[newDomain setObject:newApps forKey:#"persistent-apps"];
[self setPersistentDomain:newDomain forName:#"com.apple.dock"];
return [self synchronize];
}
return NO;
}
- (BOOL)removeApplicationFromDock:(NSString *)name {
NSDictionary *domain = [self persistentDomainForName:#"com.apple.dock"];
NSArray *apps = [domain objectForKey:#"persistent-apps"];
NSArray *newApps = [apps filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"not %K CONTAINS %#", #"tile-data.file-data._CFURLString", name]];
if (![apps isEqualToArray:newApps]) {
NSMutableDictionary *newDomain = [domain mutableCopy];
[newDomain setObject:newApps forKey:#"persistent-apps"];
[self setPersistentDomain:newDomain forName:#"com.apple.dock"];
return [self synchronize];
}
return NO;
}
#end
I found the above code from here. Code Source
the above code is getting compiled and running, however it's adding the application in the application's dock.
I am a newbie here. Please be lenient towards me as I do not have OS level of knowledge.
Many thanks in advance for your help and attention.
The code provided in question is correct the only problem was that the dock was not getting refreshed. So we just need to refresh the DOCK
In order to refresh dock I am calling a terminal command from obj-c
Terminal Command killall dock
....code...
[self runCommand:#"killall Dock"];
}
-(NSString*)runCommand:(NSString*)commandToRun;
{
NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: #"/bin/sh"];
NSArray *arguments = [NSArray arrayWithObjects:
#"-c" ,
[NSString stringWithFormat:#"%#", commandToRun],
nil];
NSLog(#"run command: %#",commandToRun);
[task setArguments: arguments];
NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
NSFileHandle *file;
file = [pipe fileHandleForReading];
[task launch];
NSData *data;
data = [file readDataToEndOfFile];
NSString *output;
output = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
return output;
}
I'm having a problem with respondsToSelector with NSMutableSet.
I have a program like this:
NSArray *arguments = [[NSProcessInfo processInfo] arguments];
theClass = [arguments objectAtIndex: 1];
theMethod = [arguments objectAtIndex: 2];
theArgument = [arguments objectAtIndex: 3];
id object = [[NSClassFromString(theClass) init] autorelease];
if([object respondsToSelector:NSSelectorFromString(theMethod)]) {
NSLog(#"Result: %#",
[object performSelector:NSSelectorFromString(theMethod) withObject: theArgument]);
} else {
NSLog(#"Class %# doesn't respond to %#.",
theClass, theMethod);
}
I call it using ./program NSMutableSet addObject: str, but the program always says that NSMutableSet doesn't respond to addObject:.
I don't know why respondsToSelector always says that NSMutableSet doesn't respond to addObject. It's the same with ./program NSMutableSet allObjects.
You were checking something completely different...
[NSClassFromString(theClass) init]
needs to be
[[NSClassFromString(theClass) alloc] init]
instead.
I wonder if it is possible to fill rangeOfString objects of a NSArray. Because I have a long list of objects for after rangeOfString:
NSArray biglist´s count is higher than list´s count.
I want to filter away the objects from the small list of the main list.
Please tell me if this is not clear.
My codes below:
NSArray *biglist = [[NSArray alloc] initWithArray:
[[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"mainlist" ofType:#"txt"]
encoding:NSUTF8StringEncoding error:NULL] componentsSeparatedByString:#"\n"]];
NSArray *list = [[NSArray alloc] initWithArray:
[[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"smalllist" ofType:#"txt"]
encoding:NSUTF8StringEncoding error:NULL] componentsSeparatedByString:#"\n"]];
for (NSString *listword in list);
NSMutableArray *wordlist = [[NSMutableArray alloc] init];
NSMutableArray *worindex = [[NSMutableArray alloc] init];
NSMutableIndexSet *mindexes = [[NSMutableIndexSet alloc] init];
NSMutableDictionary *mutdic = [[NSMutableDictionary alloc] init];
NSMutableArray *mutarray = [[NSMutableArray alloc] init];
for (NSString *s in mainlist)
{
NSRange ran = [s rangeOfString:listword];
if (ran.location !=NSNotFound)
{
//my codes here
}
}
EDIT:
I think I can solve this by writing
int i;
for (i = 0; i<[list count]; i++)
{
NSString *same = [list objectAtIndex:i];
NSLog (#"listword: %#", same);
}
But I am not sure where to place it, inside the for loop s in mainlist or outside.
EDIT: This for loop works inside the main for loop.
EDIT:
Tried these codes, but it doesnt work somehow..
NSArray *list = [[NSArray alloc] initWithArray:
[[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"small" ofType:#"txt"]
encoding:NSUTF8StringEncoding error:NULL] componentsSeparatedByString:#"\n"]];
NSArray *mainlist = [[NSArray alloc] initWithArray:
[[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"mainlist" ofType:#"txt"]
encoding:NSUTF8StringEncoding error:NULL] componentsSeparatedByString:#"\n"]];
NSMutableArray *large = [NSMutableArray arrayWithArray:mainlist];
NSArray *newlarge;
for (NSString *listword in list)
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(SELF beginswith[c] %#)",listword];
newlarge = [large filteredArrayUsingPredicate:predicate];
}
NSLog (#"large: %#", newlarge);
NSLog (#"finished!");
"I want to filter away the objects from the small list of the main list."
If I understand correctly, you want to remove an array of items from another array. You don't want to do that much work and allocations inside an n^2 loop.
This removes an array of items from another array. Depending on how large your array is you may need to optimize further but this works:
NSArray *small = [NSArray arrayWithObjects:#"three", #"two", nil];
NSMutableArray *large = [NSMutableArray arrayWithObjects:#"one", #"two", #"three", #"four", nil];
[large removeObjectsInArray:small];
// print
for (NSString *current in large)
{
NSLog(#"item: %#", current);
}
This outputs:
2011-10-13 08:39:21.176 Craplet[5235:707] item: one
2011-10-13 08:39:21.178 Craplet[5235:707] item: four
I figured it out by myself and solved this :)
It works almost perfectly.
My codes:
NSArray *big = [[NSArray alloc] initWithObjects:#"hello ->mache", #"heisann hoppsann ->hiya", #"nei men ->da", #"however ->what", #"may ->april", #"mai ->maj", nil];
NSArray *small = [[NSArray alloc] initWithObjects: #"heisann ", #"nei men ", #"however ", #"mai", nil];
NSMutableArray *smallwithh = [[NSMutableArray alloc] init];
NSMutableIndexSet *mindexes = [[NSMutableIndexSet alloc] init];
for (NSString *same in small)
{
NSLog (#"listword: %#", same);
for (NSString *s in big)
{
NSRange ran = [s rangeOfString:same];
if (ran.location !=NSNotFound)
{
[smallwithh addObject:s];
NSUInteger ind = [big indexOfObject:s];
[mindexes addIndex:ind];
}
}
}
NSLog (#"smallwith: %#", smallwithh);
[smallwithh release];
NSMutableArray *newWords =[NSMutableArray arrayWithArray: big];
[newWords removeObjectsAtIndexes: mindexes];
[big release];
[small release];
NSLog (#"newWords: %#", newWords);
I have a problem in my code which I have distilled down to the following (silly) example
NSArray *array = [NSArray arrayWithObjects:#"1", #"2", #"3", nil];
__block NSString *a = #"-1";
[array enumerateObjectsUsingBlock:^(id whoCares, NSUInteger idx, BOOL *stop) {
a = [NSString stringWithFormat:#"%# %d", a, idx];
NSLog(#"%#", a);
}];
NSLog(#"%#", a);
This code works, but if I comment out the first NSLog (within the block) the code crashes. But, if I change the format string to the following
a = [NSString stringWithFormat:#"%d", idx];
then the code runs fine without the NSLog within the block.
What is going on here? I hope I am just misunderstanding something.
stringWithFormat: gives you an autoreleased object, which you're not retaining. By the time the block exits and you call NSLog, a might have already been deallocated.
One solution might be to use a mutable string and append to it each time instead of reassigning.
NSArray *array = [NSArray arrayWithObjects:#"1", #"2", #"3", nil];
NSMutableString *a = [NSMutableString stringWithFormat:#"-1"];
[array enumerateObjectsUsingBlock:^(id whoCares, NSUInteger idx, BOOL *stop) {
[a appendFormat:#" %d", idx];
}];
NSLog(#"%#", a);
I am looking for a way to compare the contents of two NSMutableArray objects. Both arrays are filled with NSMutableDictionaries which were allocated separately but occasionally contain the same data.
Simplified Example:
NSMutableArray *firstArray = [[NSMutableArray alloc] init];
NSMutableArray *secondArray = [[NSMutableArray alloc] init];
NSMutableDictionary *a = [[NSDictionary alloc] init];
[a setObject:#"foo" forKey:"name"];
[a setObject:[NSNumber numberWithInt:1] forKey:"number"];
NSMutableDictionary *b = [[NSDictionary alloc] init];
[b setObject:#"bar" forKey:"name"];
[b setObject:[NSNumber numberWithInt:2] forKey:"number"];
NSMutableDictionary *c = [[NSDictionary alloc] init];
[c setObject:#"foo" forKey:"name"];
[c setObject:[NSNumber numberWithInt:1] forKey:"number"];
[firstArray addObject:a];
[firstArray addObject:b];
[secondArray addObject:c];
a, b and c are distinct object, but the contents of a and c match.
What I am looking for is a function / approach to compare firstArray and secondArray and return only b.
In Pseudocode:
NSArray *difference = [self magicFunctionWithArray:firstArray andArray:secondArray];
NSLog(#"%#",difference)
=> ({name="bar"; number=2})
Thank you in advance.
This can be achieved using NSMutableSet instances.
NSMutableSet * firstSet = [NSMutableSet setWithArray:firstArray];
NSMutableSet * secondSet = [NSMutableSet setWithArray:firstArray];
[firstSet unionSet:[NSSet setWithArray:secondArray]];
[secondSet intersectSet:[NSSet setWithArray:secondArray]];
[firstSet minusSet:secondSet];
NSLog(#"%#", firstSet);