Basically I'm sorting through objects in an array by y value (The furthest down the page is at the start of the array) but I'm having a bit of trouble. I assign all of the UIImageViews in an array a value:
for (UIImageView *Blocky in objectsArray){
[Blocky.layer setValue:[NSString stringWithFormat: #"%f",
Blocky.center.y] forKey:#"value"];
}
Because it's a UIImageView I have to put layer after "Blocky" otherwise I get the following error:
*** Terminating app due to uncaught exception 'NSUnknownKeyException',
reason: '[<UIImageView 0x3d44880>
setValue:forUndefinedKey:]: this class
is not key value coding-compliant for
the key value.'
My problem is when I'm sorting it I don't know where to put the ".layer" so I get the same problem because UIImageViews can't deal with keys on there own. Here is my sorting code:
NSSortDescriptor *sortDescriptor =
[[NSSortDescriptor alloc] initWithKey:#"value" ascending:YES];
[objectsArray sortUsingDescriptors:[NSArray
arrayWithObject:sortDescriptor]];
[sortDescriptor release];
Thanks in advance,
Ozzie
Use the NSMutableArray sortUsingFunction:context: method:
// top of file
static NSInteger compareImageHeights(id image1, id image2, void* context) {
CGFloat temp = ((UIImageView*)image2).center.y - ((UIImageView*)image1).center.y;
return (temp>0)?NSOrderedAscending:(temp<0)?NSOrderedDescending:NSOrderedSame;
}
// within file somewhere
[objectsArray sortUsingFunction:compareImageHeights context:NULL];
The reason the function is required (and you can't just use sortUsingSelector: or sortUsingSortDescriptor:) is that you can't specify a KVC key to identify just the y component only as a sort variable.
Edit: Changed center.y to size.height.
Edit: Oops. Changed size.height back to center.y. Fixed type to UIImageView rather than UIImage. It seems KVC objects can in theory support arbitrary keys, but some will and some won't.
Are you sure CALayer is supposed to accept arbitrary KVC keys? This looks wrong to me.
Shouldn't you extract the heights to a buffer of structures and qsort that?
eg
I think it would be something like this:
//
// before #implementation sections
//
typedef struct __MySortEntry {
UIImageView* image;
CGFloat height;
} MySortEntry, *PMySortEntry;
static int compareMySortEntry (const void * a, const void * b)
{
CGFloat temp =( ((PMySortEntry)a)->height - ((PMySortEntry)b)->height );
return (temp<0)?-1:(temp>0)?1:0;
}
//
// in #implementation section somewhere
//
NSMutableData* mutable = [[NSMutableData alloc] initWithLength:
sizeof(MySortEntry)*objectsArray.count];
PMySortEntry entries = (PMySortEntry)mutable.mutableBytes;
for (int c = 0; c < objectsArray.count; c++) {
entries[c].image = (UIImageView*)[objectsArray objectAtIndex:c];
entries[c].height = entries[c].image.center.y;
}
qsort(entries, objectArray.count, sizeof(MySortEntry), compareMySortEntry);
for (int c=0; c < objectArray.count; c++) {
UIImage* imageInSequence = entries[c].image;
// do something with images **in sequence**
}
[mutable release];
Edit: Changed center.y to size.height.
Edit: Changed size.height back to center.y. Oops.
Edit: Changed UIImage to UIImageView.
Why not set the tag property on each view as you create them based on the height and retrieve them by tag?
Related
The code is supposed to make chunks of data of an image selected from a picker view and upload it to a website , but every time i try to upload a particular chunk it gives me an EXC_BAD_ACCESS.The following is the code for splitting the image data in chunks
PrimaryImageController.h
#interface PrimaryImageViewController
{
__weak IBOutlet UIImageView *imgView;
}
#property (nonatomic,strong) NSMutableArray *chunkArray;
PrimaryImageController.m
#synthesize imgView,chunkArray;
- (void)viewDidLoad
{
chunkArray=[[NSMutableArray alloc]init];
}
-(void)updateImage
{
UIImage *img = imgView.image;
NSData *dataObj=UIImageJPEGRepresentation(img, 1.0);
NSUInteger length = [dataObj length];
NSUInteger chunkSize = 3072*10;
NSUInteger offset = 0;
int numberOfChunks=0;
do
{
NSUInteger thisChunkSize = length - offset > chunkSize ? chunkSize : length - offset;
NSData* chunk = [NSData dataWithBytesNoCopy:(char *)[dataObj bytes] + offset
length:thisChunkSize
freeWhenDone:NO];
offset += thisChunkSize;
[chunkArray insertObject:chunk atIndex:numberOfChunks];
numberOfChunks++;
}
while (offset < length);
for (int i=0; i<[chunkArray count]; i++)
{
[uploadPrimary uploadImage:[chunkArray objectAtIndex:i] uuid:uniqueIdString numberOfChunks:[chunkArray count] currentChunk:i];
}
}
exc_bad_access indicates a hard crash, no more, no less. While over-releasing objects often leads to such, there are many other reasons why such a crash might happen. As well, a hard crash is not an exception in the NSException sense; setting an exception breakpoint won't help.
If you have a crash, you should have a backtrace. Post the backtrace of the crash.
If you have ARC enabled, this looks like an interior pointer problem. You are creating a bunch of references to data contained in dataObj, but never referring to dataObj again.
Try adding [dataObj self]; after that for() loop.
However, since you are storing the chunks in an array that is an instance variable, the lifespan of dataObj should be coupled to the lifespan of that array. I.e. either move the array into the updateImage method or declare an iVar to strong reference dataObj.
I am developing an iphone application which has some data stored in a sqllite database. When my view loads i would like to load the data from the database on a background thread. The problem is the application keeps crashing and i dont know why.
The code:
-(id) init
{
if((self=[super init]))
{
[self performSelectorInBackground:#selector(loadList) withObject:nil];
}
}
-(void) loadList
{
#autoreleasepool
{
Loader * loader = [[Loader alloc] init];
NSMutableArray * array = [loader getItemList];
[array retain];
NSLog(#"Got %d items",[array count]);
[self performSelectorOnMainThread:#selector(createList:) withObject:array waitUntilDone:false];
[loader release];
}
}
-(void) createList: (NSMutableArray*) array
{
items = array;
int i;
Item * it;
for(i = 0; i < [items count]; i++)
{
it = [items objectAtIndex: i];
[it getName]; // crashes
// populate the list
}
}
Loader returns a NSMutableArray with Item objects. The application crashes when i call the item getName (which returns a NSString*). From what i understand it crashes because the item name properties is being released. What am i doing wrong?
Thanks!
It's likely to be a problem with whatever type of object you're using to populate array.
I'm unable to find finger-on-paper proof but I'm confident that performSelectorOnMainThread:withObject:waitUntilDone: retains its object. However if each of the items in array keeps a reference to loader then they need to take responsibility for retaining that object. It looks like you're attempting to keep it alive manually but — as Chuck alludes to — your call to performSelector... will return instantly and not wait for the call you've made to complete.
This particular bug appears to be that you're passing waitUntilDone:NO, so the array is being released immediately and consequently so are its items.
But in general, UIKit is not thread-safe, so this is just a touchy design. I would probably put the loading of this stuff in another class that handles the task for you instead of right in the view.
I'd put a breakpoint on the line:
it = [items objectAtIndex: i];
Then type
po it
in the debugger, and see what's in the name field. As a guess, I'd say one of two things: 1) the field that getName returns isn't initialized with an object (i.e. isn't a real NSString *) or that you're getting a C string from SQLite (which is what it usually returns) and you're trying to treat it as an NSString *. If it's the latter you can use [myCString stringWithUTF8String] to convert the C string into an NSString *
I know I can concatenate a variable name using stringwithformat, but is it possible to concatenate an object name? I'm not having any luck working around this one.
image+1.hidden = YES; for example.
If I wanted to loop through that, say 10 times, how would I create the 'image+1' part?
Thanks for any help.
I don't think that it is possible to concatenate object names in objective c, but you could create an array of images, and then reference each image like
image[0].hidden = YES;
That would fit the for loop. You could also add the images (I assume that they are UIImages) to an NSArray, then loop through like so:
NSArray* arrayOfImages;
for(UIImage* image in arrayOfImages)
{
image.hidden = YES;
}
Add the objects to an NSArray or NSMutableArray. Then loop through the array to set each object's properties.
For the purposes of discussion mainly, you can use key-value coding to set a property by its name. So, supposing you had instance, an instance of a class that provides the properties image1, image2 and image3 then you could perform:
for(int x = 1; x < 4; x++)
{
// produce the name of the property as an NSString
NSString *propertyName = [NSString stringWithFormat:#"image%d", x];
// use key-value coding to set the property
[instance setValue:someValue forKey:propertyName];
}
For the full list of accessor methods that compliant classes export, see the NSKeyValueCoding Protocol Reference. NSObject implements NSKeyValueCoding, and all properties declared as #property and implemented as #synthesize are compliant, as are any other properties with suitable accessors.
As already noted in the other answers, when what you want is an ordered list of objects so that you can do something with each in turn, either a C-style array or an NSArray is the correct way to proceed, with an NSArray being preferred for style reasons.
NSArray *array = [[NSArray alloc] initWithObjects:image1, image2, image3, image4, image5, image6, image7, image8, image9, image10, nil]; // or use NSMutableArray
for (int x = 0; x < 10; x++) {
((UIImage*)[array objectAtIndex:x]).hidden = YES;
}
I want to have a NSDictionary that maps from UIViews to something else.
However, since UIViews do not implement the NSCopying protocol, I can't use them directly as dictionary keys.
You can use an NSValue holding the pointer to the UIView and use this as key. NSValues
are copyable. but, if the view is destroyed, the NSValue will hold a
junk pointer.
Here is the actual code (based on the answer by luvieere and further suggestion by Yar):
// create dictionary
NSMutableDictionary* dict = [NSMutableDictionary new];
// set value
UIView* view = [UILabel new];
dict[[NSValue valueWithNonretainedObject:view]] = #"foo";
// get value
NSString* foo = dict[[NSValue valueWithNonretainedObject:view]];
Although this isn't really what they're intended for, you could whip up a functional dictionary-like interface using Associative References:
static char associate_key;
void setValueForUIView(UIView * view, id val){
objc_setAssociatedObject(view, &associate_key, val, OBJC_ASSOCIATION_RETAIN);
}
id valueForUIView(UIView * view){
return objc_getAssociatedObject(view, &associate_key);
}
You could even wrap this up in a class ThingWhatActsLikeADictionaryButWithKeysThatArentCopyable*; in that case you might want to retain the views that you use as keys.
Something like this (untested):
#import "ThingWhatActsLikeADictionaryButWithKeysThatArentCopyable.h"
#import <objc/runtime.h>
static char associate_key;
#implementation ThingWhatActsLikeADictionaryButWithKeysThatArentCopyable
- (void)setObject: (id)obj forKey: (id)key
{
// Remove association and release key if obj is nil but something was
// previously set
if( !obj ){
if( [self objectForKey:key] ){
objc_setAssociatedObject(key, &associate_key, nil, OBJC_ASSOCIATION_RETAIN);
[key release];
}
return;
}
[key retain];
// retain/release for obj is handled by associated objects functions
objc_setAssociatedObject(key, &associate_key, obj, OBJC_ASSOCIATION_RETAIN);
}
- (id)objectForKey: (id)key
{
return objc_getAssociatedObject(key, &associate_key);
}
#end
*The name may need some work.
Provided you don't need to support before iOS 6, NSMapTable (suggested by neilsbot) works well because it can provide an enumerator over the keys in the collection. That's handy for code common to all of the text fields, like setting the delegate or bi-directionally syncing the text values with an NSUserDefaults instance.
in viewDidLoad
self.userDefFromTextField = [NSMapTable weakToStrongObjectsMapTable];
[self.userDefFromTextField setObject:#"fooUserDefKey" forKey:self.textFieldFoo];
[self.userDefFromTextField setObject:#"barUserDefKey" forKey:self.textFieldBar];
// skipped for clarity: more text fields
NSEnumerator *textFieldEnumerator = [self.userDefFromTextField keyEnumerator];
UITextField *textField;
while (textField = [textFieldEnumerator nextObject]) {
textField.delegate = self;
}
in viewWillAppear:
NSEnumerator *keyEnumerator = [self.userDefFromTextField keyEnumerator];
UITextField *textField;
while (textField = [keyEnumerator nextObject]) {
textField.text = [self.userDefaults stringForKey:[self.textFields objectForKey:textField]];
}
in textField:shouldChangeCharactersInRange:replacementString:
NSString *resultingText = [textField.text stringByReplacingCharactersInRange:range withString:string];
if(resultingText.length == 0) return YES;
NSString *preferenceKey = [self.textFields objectForKey:textField];
if(preferenceKey) [self.userDefaults setString:resultingText forKey:preferenceKey];
return YES;
And now I will go cry, because I implemented all of this before realizing that my iOS 5.1-targeted app can't use it. NSMapTable was introduced in iOS 6.
Rather than store a pointer to the view and risk the garbage issue, just give the UIView a tag and store the tag's value in the dictionary. Much safer.
I'm using a simple solution under ARC provided by Objective-C++.
MyClass.mm:
#import <map>
#implementation MyClass
{
std::map<UIView* __weak, UIColor* __strong> viewMap;
}
- (void) someMethod
{
viewMap[self.someView] = [UIColor redColor];
}
In this example I am getting stronger type checking by making all the values have to be a UIColor* which is all I needed this for. But you could also use id as the value type if you want to allow any object as the value, ex: std::map<UIView* __weak, id __strong> viewMap; Likewise for keys: id __weak, id __strong> viewMap;
You can also vary the __strong and __weak attributes as needed. In my case, the views are already retained by the view controller that I use this in, so I saw no need to take a strong pointer to them.
a simple solution when you just want UIView as key occasionally,I use it to store UILabel and UIColor
NSArray<UIView *> *views = #[viewA,viewB,viewC,viewD];
NSArray *values = #[valueA,valueB,valueC,valueD];
for(int i = 0;i < 4;i++) {
UIView *key = views[i];
id value = values[i]
//do something
}
id value = values[[views indexOfObject:key]]
i have a problem.
When i compile my program there isn't a error but, when i start it these retern with “EXC_BAD_ACCESS”.
I looking for the error with the debug and i find it in these method but i don't understand where...
PS:the program enters in the loop sometimes.
-(void)updateMinPosition{
float valueMinX = 150;
float valueMinY = 150;
float valueMinZ = 150;
NSString *nameMinimoX = [NSString stringWithFormat:#"default"];
NSString *nameMinimoY = [NSString stringWithFormat:#"default"];
NSString *nameMinimoZ = [NSString stringWithFormat:#"default"];
for(int i = 0; i< [arrayPosizioniMovimento count]; i++){
//Posizione is my class. It contain a NSString name, 3 float valueX, valueY, valueZ
Posizione *tempPosition;
tempPosition = [[Posizione alloc]init];
tempPosition = [arrayPosizioniMovimento objectAtIndex:i];
if(tempPosition.valueX <= valueMinX){
valueMinX = tempPosition.valueX;
nameMinimoX = tempPosition.nome;
}
if(tempPosition.valueY <= valueMinY){
valueMinY = tempPosition.valueY;
nameMinimoY = tempPosition.nome;
}
if(tempPosition.valueZ <= valueMinZ){
valueMinZ = tempPosition.valueZ;
nameMinimoZ = tempPosition.nome;
}
[tempPosition dealloc];
}
labelMinX.text = nameMinimoX;
labelMinY.text = nameMinimoY;
labelMinZ.text = nameMinimoZ;
}
For the 1st glance there're several problems with your code:
Posizione *tempPosition;
tempPosition = [[Posizione alloc]init];
tempPosition = [arrayPosizioniMovimento objectAtIndex:i];
Here in the second line you create new Posizione instance and right after that assign another value to the same variable. In effect that will mean that your created instance will be never used and cause memory leak. To use element from array just write
Posizione *tempPosition = [arrayPosizioniMovimento objectAtIndex:i];
The second one - is the following line
[tempPosition dealloc];
First of all you should never call this method directly but rather send an object -release message - it will be deallocated automatically when its retain count becomes 0. In your case you do not retain tempPosition object in that code so there;s no need to release it here - just remove that line.
P.S. Using fast enumeration can also make your code more readable and less error prone:
for (Posizione *tempPosition in arrayPosizioniMovimento){
if(tempPosition.valueX <= valueMinX){
...
It looks like it may be the [tempPosition dealloc] call. You're declaring that variable and doing an alloc/init on it but then just assigning it to the object within the array, so the alloc/init is unneeded. When you make the call to dealloc at the bottom you're releasing that object that resides in the array, so your array will now have a null value which will cause the EXEC.... error
You can enable Objective-C Exception breakpoints to pinpoint the line.
On thing that jumps out is
[tempPosition dealloc];
I'm not sure what to tell you to do. You need to call release not dealloc.
[tempPosition release];
But im not sure how that fits into the rest of your code, where you are allocating a variable then immediately assigning it another value.
I think you should just remove the alloc and dealloc
so delete:
//Posizione is my class. It contain a NSString name, 3 float valueX, valueY, valueZ
Posizione *tempPosition;
tempPosition = [[Posizione alloc]init];
and
[tempPosition dealloc];
Pease show how do you initialize arrayPosizioniMovimento.
If you initialized it like arrayPosizioniMovimento = [NSMutableArray arrayWithCapacity:n]; than you'll need to add [arrayPosizioniMovimento retain];