Accessing objects when changing from NSMutableArray to NSMutableDictionary - objective-c

In one of my classes I have changed from an NSMutableArray to a NSMutableDictionary.
Before I accessed objects from other class like this:
tmpDeadline = [_taskDays[i] deadline]; //deadline is a object of another class
And accessed methods like this:
[_taskDays[datePlace]addDatedTask:d]; //addDatedTask is a method in another class
But now I can't do this anymore since I get a lot of errors which I don't really know how to handle.
What I do know is that I want use the other class's "deadline" as key and the instance of the class as object.
Here is the code (I have given the code that gives me problem the comment ERROR:
#import "LIUTaskCalendar.h"
#import "LIUTaskDay.h"
#import "LIUDatedTask.h"
#interface LIUTaskCalendar ()
{
NSMutableDictionary *_taskDays;
}
#end
#implementation LIUTaskCalendar
- (void)addDatedTasks:(LIUDatedTask *)d {
if (!_taskDays) {
_taskDays = [[NSMutableDictionary alloc] init];
}
NSInteger length = [_taskDays count];
NSDate *tmpDeadline;
NSDate *tmpDueDate;
NSInteger dateExist = 0;
NSInteger datePlace = 0;
NSDate *tmp;
for (int i = 0; i < length; i++) {
tmpDueDate = d.dueDate;
tmpDeadline = [_taskDays[i] deadline]; //*ERROR*
if ([tmpDueDate compare:tmpDeadline] == NSOrderedAscending) {
continue;
} else if ([tmpDueDate compare:tmpDeadline] == NSOrderedDescending) {
continue;
} else {
dateExist = 1;
datePlace = i;
break;
}
}
if (dateExist == 1) {
[_taskDays[datePlace]addDatedTask:d]; //*ERROR*
} else {
LIUTaskDay *tmpLIUTaskDay = [[LIUTaskDay alloc]init];
[tmpLIUTaskDay addDatedTask:d];
tmpLIUTaskDay.deadline = d.dueDate;
//[_taskDays setObject:d forKey:tmpLIUTaskDay.deadline];
[_taskDays addObject:tmpLIUTaskDay]; //*ERROR*
}
}
- (void)removeTaskDay:(NSDate *)date {
NSDate *tmpDeadline;
NSDate *tmpDeleteDate;
NSInteger dateExist = 0;
NSDate *dateDelete;
NSInteger length = [_taskDays count];
for (int i = 0; i < length; i++) {
tmpDeleteDate = date;
tmpDeadline = [_taskDays[i] deadline]; //*ERROR*
if ([tmpDeleteDate compare:tmpDeadline] == NSOrderedAscending) {
continue;
} else if ([tmpDeleteDate compare:tmpDeadline] == NSOrderedDescending) {
continue;
} else {
dateExist = 1;
break;
}
}
if (dateExist == 1) {
//[_taskDays removeObjectForKey:dateDelete];
[_taskDays removeObjectAtIndex:dateDelete]; //*ERROR*
} else {
return;
}
}
#end
If you need me to provide the code for the other class to then don't
hesitate to tell me.
Thanks in advance
UPDATE
Changed from this:
[_taskDays addObject:tmpLIUTaskDay];
To this:
[_taskDays setObject:d forKey:tmpLIUTaskDay.deadline];

taskDays is dictionary, while you are using it as if it is an array _taskDays[i] in
tmpDeadline = [_taskDays[i] deadline]; //*ERROR*
also here:
[_taskDays addObject:tmpLIUTaskDay]; //*ERROR*
To fetch from dictionary or add a new key-value pair you should do like this:
tmpDeadline = [_taskDays[#"taskDay"] deadline];
and
[_taskDays addObject:addObject:tmpLIUTaskDay forKey:#"taskDay"];

Related

Objective-C. access to property of item in array which consists of objects

I have the array which consists of objects:
ViewController *item1 = [ViewController new];
item1.name = #"Mary";
item1.Description = #"good girl";
ViewController *item2 = [ViewController new];
item2.name = #"Daniel";
item2.Description = #"bad boy";
ComplexArray= [NSArray arrayWithObjects: item1, item2, nil];`
i want to view in labels a name and description if name is equal Mary
for (int i = 0; i < [ComplexArray count]; i++) {
if (item[i].name isEqualString:#"Mary") {
_nameLabel.text= item[i].name;
_DescriptionLabel.text= item[i].Description;
}
}
Please help me
You basically had it. All I did was rename item to ComplexArray added [] around the isEqualToString call and added a break:
for (int i = 0; i < [ComplexArray count]; i++) {
ViewController *item = ComplexArray[i];
if ([item.name isEqualString:#"Mary"]) {
_nameLabel.text= item.name;
_DescriptionLabel.text= item.Description;
break; // Added
}
}
There are other ways, but this approach is fine.
BTW: variables should start, by convention, with a lowercase character.
Your problem is you didn't assign anything in item variable. Just update like this and it will work.
for (int i = 0; i < [ComplexArray count]; i++) {
ViewController *item = [ComplexArray objectAtIndex:i]; // you missed this line.
if ([item.name isEqualToString:#"Mary"]) { //you missed the opening "[" and closing "]"
_nameLabel.text= item.name;
_DescriptionLabel.text= item.Description;
}
}

Can not pass arguments to CFSwapInt16LittleToHost correctly in Swift

I am converting code from Objective-C to Swift and am having trouble with the casting especially for byte operations. In particular, I am getting the error 'Can not invoke CFSwapInt16LittleToHost with an argument list of (Int16)'. The Objective-C code is:
#interface THSampleDataFilter ()
#property (nonatomic, strong) NSData *sampleData;
#end
#implementation THSampleDataFilter
- (id)initWithData:(NSData *)sampleData {
self = [super init];
if (self) {
_sampleData = sampleData;
}
return self;
}
- (NSArray *)filteredSamplesForSize:(CGSize)size {
NSMutableArray *filteredSamples = [[NSMutableArray alloc] init];
NSUInteger sampleCount = self.sampleData.length / sizeof(SInt16);
NSUInteger binSize = sampleCount / size.width;
SInt16 *bytes = (SInt16 *) self.sampleData.bytes;
SInt16 maxSample = 0;
for (NSUInteger i = 0; i < sampleCount; i += binSize) {
SInt16 sampleBin[binSize];
for (NSUInteger j = 0; j < binSize; j++) {
sampleBin[j] = CFSwapInt16LittleToHost(bytes[i + j]);
}
SInt16 value = [self maxValueInArray:sampleBin ofSize:binSize];
[filteredSamples addObject:#(value)];
if (value > maxSample) {
maxSample = value;
}
}
My Swift code is:
class THSampleDataFilter : NSObject
{
var sampleData: NSData?
init(sampleData: NSData)
{
super.init()
self.sampleData = sampleData
}
func filteredSamplesForSize(size: CGSize) -> NSArray?
{
var filteredSamples = NSMutableArray()
var sampleCount:UInt = UInt(Int(self.sampleData!.length) / Int(sizeof(Int16)))
var binSize = UInt(sampleCount / UInt(size.width))
var bytes: UnsafePointer<Int16> = UnsafePointer<Int16>(self.sampleData!.bytes)
var maxSample: Int16 = 0
for var i:UInt = 0; i < sampleCount; i = i + binSize
{
var sampleBin: [Int16] = []
for var j: UInt = 0; j < binSize; j++
{
sampleBin.append(CFSwapInt16LittleToHost(bytes[i + j]))
}
let value: Int16 = self.maxValueInArray(sampleBin, size: binSize)
filteredSamples.addObject(NSNumber(short: value))
if value > maxSample
{
maxSample = value
}
}
The problem line is:
sampleBin.append(CFSwapInt16LittleToHost(bytes[i + j]))
This is when I get the error 'Can not invoke CFSwapInt16LittleToHost with an argument list of (Int16)'. I have tried every combination I can think of.
Any help would be greatly appreciated!
Thank you for looking.
CFSwapInt16LittleToHost takes a UInt16, not an Int16.
sampleBin.append(CFSwapInt16LittleToHost(UInt16(bytes[i + j])))
or change bytes to a UInt16 pointer.
var bytes: UnsafePointer<UInt16> = UnsafePointer<UInt16>(self.sampleData!.bytes)

Algorithm to find all possible solutions from an array of array

What is the best algorithm to find all possible words from an array of array of character.
Here an example :
From this array : [[A],[B,C,D],[E,F],[G,H]]
I need in return an array of the 12 ordered possibilities [[A,B,E,G],[A,C,E,G], ... , [A,D,F,H]]
Do you know how to implement this algorithm ? If you know it and you provide an example in any language (C,JAVA,Javascript, ...), feel free to share because it's been a day I try to find it ...
Here how I tries to implement it ("array" is an array of array of char):
+ (NSArray*) possibleReading:(NSMutableArray*)array {
int nbPossibilities = 1;
for(int i = 0; i < [array count]; i++) {
nbPossibilities *=[[array objectAtIndex:i] count];
}
NSMutableArray *possArr = [[NSMutableArray alloc] initWithCapacity:nbPossibilities];
for (int i=0; i < nbPossibilities; i++) {
NSMutableArray *innerArray = [[NSMutableArray alloc] initWithCapacity:[array count]];
[possArr addObject:innerArray];
}
for (int i=0; i< [array count]; i++) {
//
for(int nbPoss = 0; nbPoss < nbPossibilities; nbPoss++) {
NSMutableArray * arr = [possArr objectAtIndex:nbPoss];
NSNumber * num = [NSNumber numberWithInt:nbPoss % [[array objectAtIndex:i] count]];
NSString * literal = [[array objectAtIndex:i] objectAtIndex:[num intValue]];
[arr insertObject:literal atIndex:i];
}
}
return possArr;
}
It would be easiest to do this using a recursive method.
Java code
import java.util.Arrays;
public class CartesianProductCalculator {
private char[][] result;
private char[][] sets;
private char[] currentSet;
private int index;
public char[][] calculateProduct(char[][] sets) {
index = 0;
// calculate size of result
int resultSize = 1;
this.sets = sets;
for (char[] set : sets) {
resultSize *= set.length;
}
result = new char[resultSize][];
currentSet = new char[sets.length];
calculateProduct(sets.length-1);
return result;
}
// fills result from right to left
public void calculateProduct(int setIndex) {
if (setIndex >= 0) {
for (char c : sets[setIndex]) {
currentSet[setIndex] = c;
calculateProduct(setIndex-1);
}
} else {
result[index++] = Arrays.copyOf(currentSet, currentSet.length);
}
}
public static void main(String[] args) {
char[][] input = {{'A'},{'B','C','D'},{'E','F'},{'G','H'}};
CartesianProductCalculator productCalculator = new CartesianProductCalculator();
System.out.println(Arrays.deepToString(productCalculator.calculateProduct(input)));
}
}
Objectiv-C
+ (NSArray *) cartesianProductOfArrays(NSArray *arrays) {
int arraysCount = arrays.count;
unsigned long resultSize = 1;
for (NSArray *array in arrays)
resultSize *= array.count;
NSMutableArray *product = [NSMutableArray arrayWithCapacity:resultSize];
for (unsigned long i = 0; i < resultSize; ++i) {
NSMutableArray *cross = [NSMutableArray arrayWithCapacity:arraysCount];
[product addObject:cross];
unsigned long n = i;
for (NSArray *array in arrays) {
[cross addObject:[array objectAtIndex:n % array.count]];
n /= array.count;
}
}
return product;
}
C
#include <stdio.h>
#include <string.h>
void print(int size, char *array[size], int indexs[size]){
char result[size+1];
int i;
for(i = 0; i < size; ++i)
result[i] = array[i][indexs[i]];
result[size] = 0;
puts(result);
}
int countUp(int size, int indexs[size], int lens[size]){
int i = size -1;
while(i >= 0){
indexs[i] += 1;// count up
if(indexs[i] == lens[i])
indexs[i--] = 0;
else
break;
}
return i >= 0;
}
void find_all(int size, char *array[size]){
int lens[size];
int indexs[size];
int i;
for(i = 0; i < size; ++i){//initialize
lens[i] = strlen(array[i]);
indexs[i] = 0;
}
do{
print(size, array, indexs);
}while(countUp(size, indexs, lens));
}
int main(void){
char *array[] = { "A", "BCD", "EF", "GH" };
int size = sizeof(array)/sizeof(*array);
find_all(size, array);
return 0;
}
If you can remove duplicate entries in inner array objects before executing method then you won't get duplicate words in result array.
- (NSArray*) possibleReading:(NSMutableArray*)array {
int nbPossibilities = 1;
for(int i = 0; i < [array count]; i++)
{
NSArray *cleanedArray = [[NSSet setWithArray:[array objectAtIndex:i]] allObjects];
[array replaceObjectAtIndex:i withObject:cleanedArray];
nbPossibilities *=[[array objectAtIndex:i] count];
}
NSMutableArray *possArr = [[NSMutableArray alloc] initWithCapacity:nbPossibilities];
for (int i=0; i < nbPossibilities; i++) {
NSMutableArray *innerArray = [[NSMutableArray alloc] initWithCapacity:[array count]];
[possArr addObject:innerArray];
}
for (int i=0; i< [array count]; i++) {
//
for(int nbPoss = 0; nbPoss < nbPossibilities; nbPoss++) {
NSMutableArray * arr = [possArr objectAtIndex:nbPoss];
NSNumber * num = [NSNumber numberWithInt:nbPoss % [[array objectAtIndex:i] count]];
NSString * literal = [[array objectAtIndex:i] objectAtIndex:[num intValue]];
[arr insertObject:literal atIndex:i];
}
}
return possArr;
}

Generating primes List in objective C?

I create this objective C class to Genrate prime numbers from n to limit. I have problem I could not get the item in NSMutableArray inside for loop. can some one show me how can I fix it?
#implementation Prime
-(NSMutableArray *)generatePrimes:(int)upperLimit{
NSMutableArray *primes = [[NSMutableArray alloc]init];
bool isPrime;
int j;
[primes addObject:[NSDecimalNumber numberWithInt:2]];
for (int i= 3; i <= upperLimit ; i+=2) {
j = 0;
isPrime = YES;
NSInteger index;
for(id obj in primes)
{
index = [primes indexOfObject:obj];
if((index * index) <= i )
{
if(i % index == 0)
{
isPrime = NO;
break;
}
}
}
if(isPrime)
{
[primes addObject:[NSDecimalNumber numberWithInt:i]];
}
}
return primes;
}
#end
This question is a little vague, but it seems to me what you're trying to do is convert the NSDecimalNumber back into an int. What your code is actually doing is getting the number's index inside the array (ie the first object is 0, the second is 1, etc.) If you are trying to get the original value of i, change these lines:
for(id obj in primes)
{
index = [primes indexOfObject:obj];
to this:
for(NSDecimalNumber num in primes)
{
index = [num integerValue];
I'd also recommend using a different name than index, as that's misleading as to what you're actually doing.
-(NSMutableArray *)generatePrimes:(int)upperLimit
{
NSMutableArray *primes = [[NSMutableArray alloc]init];
bool isPrime;
for (int i=2; i<upperLimit; i++)
{
bool prime = true;
for (int j=2; j*j<=i; j++)
{
if (i % j == 0)
{
prime = false;
break;
}
}
if(prime)
{
[primes addObject:[NSDecimalNumber numberWithInt:i]];
}
}
return primes;
}
or
-(NSMutableArray *)generatePrimes:(int)upperLimit
{
NSMutableArray *primes = [[NSMutableArray alloc]init];
[primes addObject:[NSDecimalNumber numberWithInt:2]];
for(int i=3; i < upperLimit; i++)
{
bool prime=true;
for(int j=0;j<primes.count && (((NSDecimalNumber *)primes[j]).integerValue*((NSDecimalNumber *)primes[j]).integerValue) <= i;j++)
{
if(i % (((NSDecimalNumber *)primes[j]).integerValue) == 0)
{
prime=false;
break;
}
}
if(prime)
{
[primes addObject:[NSDecimalNumber numberWithInt:i]];
}
}
return primes;
}
Hope this helps!

Anagram algorithm objective C

i have written the following code to check anagram want to know is this perfect & is there any better way to implement the same in objective C
-(BOOL) findAnagram :(NSString *) string1 :(NSString *) string2
{
int len = string1.length;
if (len != string2.length)
{
return false;
}
for (int i=0; i < len; i++)
{
int h = 0;
int q = 0;
for (int k = 0; k < len ; k ++)
{
if ([string1 characterAtIndex:i] == [string1 characterAtIndex:k])
{
h++;
}
if ([string1 characterAtIndex:i] == [string2 characterAtIndex:k])
{
q++;
}
}
if (h!=q)
{
return false;
}
}
return TRUE;
}
A better performing version than yours, which is a O(n ^ 2) algorithm, is a O(n) algorithm:
BOOL anagrams(NSString *a, NSString *b)
{
if (a.length != b.length)
return NO;
NSCountedSet *aSet = [[NSCountedSet alloc] init];
NSCountedSet *bSet = [[NSCountedSet alloc] init];
for (int i = 0; i < a.length; i++)
{
[aSet addObject:#([a characterAtIndex:i])];
[bSet addObject:#([b characterAtIndex:i])];
}
return [aSet isEqual:bSet];
}
You want to know if two strings contain exactly the same characters? Easiest way would probably be to sort both of them and compare the sorted version.
Another way would be to count the number of appearances of each letter (how many As, how many Bs, and so forth), then compare those counts.
(Note: The second way is just a variation of the first one, it's one efficient way to sort a string)
It looks fine to me. But the code style is slightly odd. I would write it like this:
- (BOOL)isStringAnagram:(NSString *)string1 ofString:(NSString *)string2 {
int len = string1.length;
if (len != string2.length) {
return NO;
}
for (int i=0; i < len; i++) {
int h = 0;
int q = 0;
for (int k = 0; k < len; k++) {
if ([string1 characterAtIndex:i] == [string1 characterAtIndex:k]) {
h++;
}
if ([string1 characterAtIndex:i] == [string2 characterAtIndex:k]) {
q++;
}
}
if (h != q) {
return NO;
}
}
return YES;
}
The main issue I have is with the method name. While it's possible to have parameters that have nothing before them in the name, it is not advisable. i.e. you had findAnagram:: as the name whereas I've used isStringAnagram:ofString:.
This is an implementation on #zmbq suggestion of sorting and comparing.
You should consider the requirements of deleting spaces and being case insensitive.
- (BOOL)isAnagram:(NSString *)leftString and:(NSString *)rightString {
NSString *trimmedLeft = [[leftString stringByReplacingOccurrencesOfString:#" " withString:#""] lowercaseString];
NSString *trimmedRight = [[rightString stringByReplacingOccurrencesOfString:#" " withString:#""] lowercaseString];
return [[self stringToCharArraySorted:trimmedLeft] isEqual:[self stringToCharArraySorted:trimmedRight]];
}
- (NSArray *)stringToCharArraySorted:(NSString *)string {
NSMutableArray *array = [[NSMutableArray alloc] init];
for (int i = 0 ; i < string.length ; i++) {
[array addObject:#([string characterAtIndex:i])];
}
return [[array sortedArrayUsingSelector:#selector(compare:)] copy];
}
called like this
BOOL isAnagram = [self isAnagram:#"A BC" and:#"cba"];
Check the following method which check Anagram strings.
-(BOOL)checkAnagramString:(NSString*)string1 WithAnotherString:(NSString*)string2{
NSCountedSet *countSet1=[[NSCountedSet alloc]init];
NSCountedSet *countSet2=[[NSCountedSet alloc]init];
if (string1.length!=string2.length) {
NSLog(#"NOT ANAGRAM String");
return NO;
}
for (int i=0; i<string1.length; i++) {
[countSet1 addObject:#([string1 characterAtIndex:i])];
[countSet2 addObject:#([string2 characterAtIndex:i])];
}
if ([countSet1 isEqual:countSet2]) {
NSLog(#"ANAGRAM String");
return YES;
} else {
NSLog(#"NOT ANAGRAM String");
return NO;
}
}
Another run of the mill algorithm:
- (BOOL) testForAnagramWithStrings:(NSString *)stringA andStringB: (NSString *)stringB{
stringA = [stringA lowercaseString];
stringB = [stringB lowercaseString];
int counter = 0;
for (int i=0; i< stringA.length; i++){
for (int j=0; j<stringB.length;j++){
if ([stringA characterAtIndex:i]==[stringB characterAtIndex:j]){
counter++;
}
}
}
if (counter!= stringA.length){
return false;
}
return true;
}