The following code works, but produces warnings about non-initialized variables.
The code parses a string representing a molecule, such as #"2C12;5H1;1O16". It checks that it is correctly formatted and that all the components are valid isotopes. As listed, it works perfectly, but produces warnings from the compiler.
//listOfNames is a global variable, an NSArray containing strings of all isotope names i.e. #"Na23"
-(bool)canGrokSpeciesName:(NSString*)theSpeciesName{
//Test that the species name makes sense
//Names should be of the form of repeated #"nnSSaa" with nn being the multiplier, SS being the element name, aa being the atomic mass number
bool couldGrok = [theSpeciesName isNotEqualTo:#""];
if(couldGrok){
NSArray* listOfAtomsWithMultiplier = [theSpeciesName componentsSeparatedByString:#";"];
for (int i=0; i<[listOfAtomsWithMultiplier count]; i++) NSLog(#"%#", [listOfAtomsWithMultiplier objectAtIndex:i]);
NSInteger multiplet[ [listOfAtomsWithMultiplier count] ];
bool ok[ [listOfAtomsWithMultiplier count] ];
bool noFail = true;
for (int i=0; i<[listOfAtomsWithMultiplier count]; i++) {
NSScanner* theScanner = [NSScanner scannerWithString:[listOfAtomsWithMultiplier objectAtIndex:i]];
NSString* multipletString;
if(![theScanner isAtEnd]) [theScanner scanUpToCharactersFromSet:[NSCharacterSet letterCharacterSet] intoString:&multipletString];
multiplet[i] = [multipletString integerValue];
NSString* atomString;
if(![theScanner isAtEnd]) [theScanner scanUpToCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet] intoString:&atomString];
NSInteger A;
if(![theScanner isAtEnd]) [theScanner scanInteger:&A];
NSLog(#"%#%i with multiplet of %i", atomString, (int)A, (int)multiplet[i]);
bool hasBeenFound = false;
for(int j=0; j<[listOfNames count]; j++){
if(noFail) hasBeenFound = hasBeenFound || [[listOfNames objectAtIndex:j] isEqualToString:[NSString stringWithFormat:#"%#%i", atomString, (int)A]];
}
couldGrok = couldGrok && noFail && hasBeenFound;
}
}
return couldGrok;
}
However, if I initialize with
NSString* multipletString = #"";
NSString* atomString = #"";
NSInteger A = -1;
then the NSScanner returns them with their originally initialized values. Am I missing something basic?
I prefer not to ignore warnings from the compiler, but following the compiler suggestions breaks the code. So, I suppose this is more a request for basic information than a request for how to fix my code. Thanks!
Related
Is it possible to activate (bring to the fore) a window based on the values returned from CGWindowListCopyWindowInfo? (i.e Using the window ID (kCGWindowNumber) or something else.)
Edit:
I should specify that my app (which would run with accessibility permissions) needs to be able to do this for windows of other apps.
Since posting the question I've discovered AXUIElementPerformAction. Am I going in the right direction with this?
Or is running AppleScript bridge within my code the best approach?
You can attach to a process by pid and get its windows. Then use kAXRaiseAction to bring them to front, like this:
AXUIElementRef element = AXUIElementCreateApplication(pid);
if (element) {
CFArrayRef array;
AXUIElementCopyAttributeValues(element, kAXWindowsAttribute, 0, 99999, &array);
if (array == nullptr)
return;
NSArray *windows = (NSArray *)CFBridgingRelease(array);
for (NSUInteger i = 0; i < windows.count; ++i) {
AXUIElementRef ref = (__bridge AXUIElementRef)(windows[i]);
AXError error = AXUIElementPerformAction(ref, kAXRaiseAction);
// handle error
}
}
CFRelease(element);
No need to release array or windows. Children in arrays are handled automatically and the array is bridged to an NSArray which is released by ARC.
My answer's a little overcomplicated compared to what was already shared by Mike Lischke, but I've already posted it on a different SO question and I think it is a tiny bit closer to what you need:
#import <Cocoa/Cocoa.h>
#import <libproc.h>
#import <string.h>
#import <stdlib.h>
#import <stdio.h>
bool activate_window_of_id(unsigned long wid) {
bool success = false;
const CGWindowLevel kScreensaverWindowLevel = CGWindowLevelForKey(kCGScreenSaverWindowLevelKey);
CFArrayRef windowArray = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, kCGNullWindowID);
CFIndex windowCount = 0;
if ((windowCount = CFArrayGetCount(windowArray))) {
for (CFIndex i = 0; i < windowCount; i++) {
NSDictionary *windowInfoDictionary = (__bridge NSDictionary *)((CFDictionaryRef)CFArrayGetValueAtIndex(windowArray, i));
NSNumber *ownerPID = (NSNumber *)(windowInfoDictionary[(id)kCGWindowOwnerPID]);
NSNumber *level = (NSNumber *)(windowInfoDictionary[(id)kCGWindowLayer]);
if (level.integerValue < kScreensaverWindowLevel) {
NSNumber *windowID = windowInfoDictionary[(id)kCGWindowNumber];
if (wid == windowID.integerValue) {
CFIndex appCount = [[[NSWorkspace sharedWorkspace] runningApplications] count];
for (CFIndex j = 0; j < appCount; j++) {
if (ownerPID.integerValue == [[[[NSWorkspace sharedWorkspace] runningApplications] objectAtIndex:j] processIdentifier]) {
NSRunningApplication *appWithPID = [[[NSWorkspace sharedWorkspace] runningApplications] objectAtIndex:j];
[appWithPID activateWithOptions:NSApplicationActivateAllWindows | NSApplicationActivateIgnoringOtherApps];
char buf[PROC_PIDPATHINFO_MAXSIZE];
proc_pidpath(ownerPID.integerValue, buf, sizeof(buf));
NSString *buffer = [NSString stringWithUTF8String:buf];
unsigned long location = [buffer rangeOfString:#".app/Contents/MacOS/" options:NSBackwardsSearch].location;
NSString *path = (location != NSNotFound) ? [buffer substringWithRange:NSMakeRange(0, location)] : buffer;
NSString *app = [#" of application \\\"" stringByAppendingString:[path lastPathComponent]];
NSString *index = [#"set index of window id " stringByAppendingString:[windowID stringValue]];
NSString *execScript = [[index stringByAppendingString:app] stringByAppendingString:#"\\\" to 1"];
char *pointer = NULL;
size_t buffer_size = 0;
NSMutableArray *array = [[NSMutableArray alloc] init];
FILE *file = popen([[[#"osascript -e \"" stringByAppendingString:execScript] stringByAppendingString:#"\" 2>&1"] UTF8String], "r");
while (getline(&pointer, &buffer_size, file) != -1)
[array addObject:[NSString stringWithUTF8String:pointer]];
char *error = (char *)[[array componentsJoinedByString:#""] UTF8String];
if (strlen(error) > 0 && error[strlen(error) - 1] == '\n')
error[strlen(error) - 1] = '\0';
if ([[NSString stringWithUTF8String:error] isEqualToString:#""])
success = true;
[array release];
free(pointer);
pclose(file);
break;
}
}
}
}
}
}
CFRelease(windowArray);
return success;
}
The code it is based on does not work as advertised for its original purpose. Although, it did help me a lot to get working all the stuff I needed to answer this question. The code my answer is based on can be found here.
I have an NSMutableArray with items in it.
I would like to compare every item with a string.
If the string is the same then the next item of the array should be stored in another array.
NSString *string2 = [NSString stringWithFormat:#"Period:"];
// Filtern nach Periode
NSMutableArray *Eventarray =[NSMutableArray array];
for(int i=0;i<[lines count]; i++)
{
NSMutableString *string1 = [NSMutableString stringWithFormat: #"%#",[[lines objectAtIndex:i]description]];
// NSLog(#"%#",string1);
int index = [[lines objectAtIndex:i] indexOfObject:#"Period:"];
NSLog(#"%#",index);
if ([[lines objectAtIndex:i] isEqualToString:#"Period:"])
{
//strings are same
NSLog(#"ii");
NSLog(#"ii");
int e=i+1;
NSMutableString *Periode = [NSMutableString stringWithFormat: #"%#",[lines objectAtIndex:e]];
[Eventarray addObject:Periode];
}
[string1 deleteCharactersInRange:NSMakeRange([string1 length]-1, 1)];
}
for(int i=0;i<[Eventarray count]; i++)
{
NSLog(#"Eventarray: %#", [Eventarray objectAtIndex:i]);
}
The array looks like this:
2013-08-12 13:31:35.375 xxxx[3809:207] PublishedRoster
2013-08-12 13:31:35.376 xxxx[3809:207] Period:
2013-08-12 13:31:35.377 xxxx[3809:207] 25Jul2013-08Sep2013
I tried everything but I don't know whats wrong. What am I missing?
Try using NSRange
If you want to search for a substring
NSString *homebrew = #"Rajneesh071";
NSRange range = [homebrew rangeOfString:#"Raj"];
// Did we find the string "Raj" ?
if (range.length > 0)
NSLog(#"Range is: %#", NSStringFromRange(range));
In your code you can do it like.
for(int i=0;i<[lines count]; i++)
{
NSRange stringRange = [[lines objectAtIndex:i] rangeOfString:#"Period:" options:NSCaseInsensitiveSearch];
if(stringRange.location != NSNotFound)
{
[Eventarray addObject:Periode];
}
}
I'm trying to re-arrange words into alphabetical order. For example, tomato would become amoott, or stack would become ackst.
I've found some methods to do this in C with char arrays, but I'm having issues getting that to work within the confines of the NSString object.
Is there an easier way to do it within the NSString object itself?
You could store each of the string's characters into an NSArray of NSNumber objects and then sort that. Seems a bit expensive, so I would perhaps just use qsort() instead.
Here it's provided as an Objective-C category (untested):
NSString+SortExtension.h:
#import <Foundation/Foundation.h>
#interface NSString (SortExtension)
- (NSString *)sorted;
#end
NSString+SortExtension.m:
#import "NSString+SortExtension.h"
#implementation NSString (SortExtension)
- (NSString *)sorted
{
// init
NSUInteger length = [self length];
unichar *chars = (unichar *)malloc(sizeof(unichar) * length);
// extract
[self getCharacters:chars range:NSMakeRange(0, length)];
// sort (for western alphabets only)
qsort_b(chars, length, sizeof(unichar), ^(const void *l, const void *r) {
unichar left = *(unichar *)l;
unichar right = *(unichar *)r;
return (int)(left - right);
});
// recreate
NSString *sorted = [NSString stringWithCharacters:chars length:length];
// clean-up
free(chars);
return sorted;
}
#end
I think separate the string to an array of string(each string in the array contains only one char from the original string). Then sort the array will be OK. This is not efficient but is enough when the string is not very long. I've tested the code.
NSString *str = #"stack";
NSMutableArray *charArray = [NSMutableArray arrayWithCapacity:str.length];
for (int i=0; i<str.length; ++i) {
NSString *charStr = [str substringWithRange:NSMakeRange(i, 1)];
[charArray addObject:charStr];
}
NSString *sortedStr = [[charArray sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)] componentsJoinedByString:#""];
// --------- Function To Make an Array from String
NSArray *makeArrayFromString(NSString *my_string) {
NSMutableArray *array = [[NSMutableArray alloc] init];
for (int i = 0; i < my_string.length; i ++) {
[array addObject:[NSString stringWithFormat:#"%c", [my_string characterAtIndex:i]]];
}
return array;
}
// --------- Function To Sort Array
NSArray *sortArrayAlphabetically(NSArray *my_array) {
my_array= [my_array sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
return my_array;
}
// --------- Function Combine Array To Single String
NSString *combineArrayIntoString(NSArray *my_array) {
NSString * combinedString = [[my_array valueForKey:#"description"] componentsJoinedByString:#""];
return combinedString;
}
// Now you can call the functions as in below where string_to_arrange is your string
NSArray *blowUpArray;
blowUpArray = makeArrayFromString(string_to_arrange);
blowUpArray = sortArrayAlphabetically(blowUpArray);
NSString *arrayToString= combineArrayIntoString(blowUpArray);
NSLog(#"arranged string = %#",arrayToString);
Just another example using NSMutableString and sortUsingComparator:
NSMutableString *mutableString = [[NSMutableString alloc] initWithString:#"tomat"];
[mutableString appendString:#"o"];
NSLog(#"Orignal string: %#", mutableString);
NSMutableArray *charArray = [NSMutableArray array];
for (int i = 0; i < mutableString.length; ++i) {
[charArray addObject:[NSNumber numberWithChar:[mutableString characterAtIndex:i]]];
}
[charArray sortUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
if ([obj1 charValue] < [obj2 charValue]) return NSOrderedAscending;
return NSOrderedDescending;
}];
[mutableString setString:#""];
for (int i = 0; i < charArray.count; ++i) {
[mutableString appendFormat:#"%c", [charArray[i] charValue]];
}
NSLog(#"Sorted string: %#", mutableString);
Output:
Orignal string: tomato
Sorted string: amoott
I have this function:
void myFunc(NSString* data) {
NSMutableArray *instrs = [[NSMutableArray alloc] initWithCapacity:[data length]];
for (int i=0; i < [data length]; i++) {
unichar c = [data characterAtIndex:i];
[instrs addObject:c];
}
NSEnumerator *e = [instrs objectEnumerator];
id inst;
while (inst = [e nextObject]) {
NSLog("%i\n", inst);
}
}
I think it fails at [instrs addObject:c]. It's purpose is to iterate through the hexadecimal numbers of an NSString. What causes this code to fail?
A unichar is not an object; it's an integer type.
NSMutableArray can only hold objects.
If you really want to put it into an NSMutableArray, you could wrap the integer value in an NSNumber object: [instrs addObject:[NSNumber numberWithInt:c]];
But, what's the point of stuffing the values into an array in the first place? You know how to iterate through the string and get the characters, why put them into an array just to iterate through them again?
Also note that:
the "%i" NSLog format expects an integer; you can't pass it an object
for hexadecimal output, you want "%x", not "%i"
If the function is only meant to display the characters as hexadecimal values, you could use:
void myFunc(NSString* data)
{
NSUInteger len = [data length];
unichar *buffer = calloc(len, sizeof(unichar));
if (!buffer) return;
[data getCharacters:buffer range:NSMakeRange(0, len)];
for (NSUInteger i = 0; i < len; i++)
NSLog(#"%04x", (unsigned) buffer[i]);
free(buffer);
}
This is just a little bit more efficient than your approach (also, in your approach you never release the instrs array, so it will leak in a non-garbage-collected environment).
If the string contains hexadecimal numbers, then you will want to repeatedly use an NSScanner's scanHexInt: method until it returns NO.
void myFunc(NSString* data)
{
NSScanner *scanner = [[NSScanner alloc] initWithString:data];
unsigned number;
while ([scanner scanHexInt:&number])
NSLog(#"%u", number);
[scanner release];
}
Here's what I want to do. I have 2 strings and I want to determine if one string is a permutation of another. I was thinking to simply remove the characters from string A from string B to determine if any characters are left. If no, then it passes.
However, I need to make sure that only 1 instance of each letter is removed (not all occurrences) unless there are multiple letters in the word.
An example:
String A: cant
String B: connect
Result: -o-nec-
Experimenting with NSString and NSScanner has yielded no results so far.
Hmmm, let's have a go:
NSString *stringA = #"cant";
NSString *stringB = #"connect";
NSUInteger length = [stringB length];
NSMutableCharacterSet *charsToRemove = [NSMutableCharacterSet characterSetWithCharactersInString:stringA];
unichar *buffer = calloc(length, sizeof(unichar));
[stringB getCharacters:buffer range:NSMakeRange(0, length)];
for (NSUInteger i = 0; i < length; i++)
{
if ([charsToRemove characterIsMember:buffer[i]])
{
[charsToRemove removeCharactersInRange:NSMakeRange(buffer[i], 1)];
buffer[i] = '-';
}
}
NSString *result = [NSString stringWithCharacters:buffer length:length];
free (buffer);
An inefficient yet simple way might be something like this (this is implemented as a category on NSString, but it could just as easily be a method or function taking two strings):
#implementation NSString(permutation)
- (BOOL)isPermutation:(NSString*)other
{
if( [self length] != [other length] ) return NO;
if( [self isEqualToString:other] ) return YES;
NSUInteger length = [self length];
NSCountedSet* set1 = [[[NSCountedSet alloc] initWithCapacity:length] autorelease];
NSCountedSet* set2 = [[[NSCountedSet alloc] initWithCapacity:length] autorelease];
for( int i = 0; i < length; i++ ) {
NSRange range = NSMakeRange(i, 1);
[set1 addObject:[self substringWithRange:range]];
[set2 addObject:[self substringWithRange:range]];
}
return [set1 isEqualTo:set2];
}
#end
This returns what your example asks for...
NSString* a = #"cant";
NSString* b = #"connect";
NSMutableString* mb = [NSMutableString stringWithString:b];
NSUInteger i;
for (i=0; i<[a length]; i++) {
NSString* theLetter = [a substringWithRange:NSMakeRange(i, 1)];
NSRange r = [mb rangeOfString:theLetter];
if (r.location != NSNotFound) {
[mb replaceCharactersInRange:r withString:#"-"];
}
}
NSLog(#"mb: %#", mb);
However, I wouldn't call that a permutation. To me a permutation would only hold true if all the characters from string "a" were contained by string "b". In your example, since the letter a in cant isn't in string b then I would say that cant is not a permutation of connect. With this definition I would use this:
-(BOOL)isString:(NSString*)firstString aPermutationOfString:(NSString*)secondString {
BOOL isPermutation = YES;
NSMutableString* mb = [NSMutableString stringWithString:secondString];
NSUInteger i;
for (i=0; i<[firstString length]; i++) {
NSString* theLetter = [firstString substringWithRange:NSMakeRange(i, 1)];
NSRange r = [mb rangeOfString:theLetter];
if (r.location != NSNotFound) {
[mb deleteCharactersInRange:r];
} else {
return NO;
}
}
return isPermutation;
}