I'm trying to change Xcode's auto-formatting of the NSDictionary to conform to a style guide:
Xcode behavior:
NSDictionary *resourceResponseDictionary = #{
#"status": #"OK",
#"result": [NSDictionary dictionary]
};
Desired behavior:
NSDictionary *resourceResponseDictionary = #{
#"status": #"OK",
#"result": [NSDictionary dictionary]
};
Where would I begin to look to make the change? Is it even possible?
By auto-formatting I guess you mean 'Indentation'. I had a similar question/problem and came to realize that Xcode does not have a specific setting for this except the basic ones in preferences. For example, when adding multiple <Delegates, DataSource...>, starting a new line has no indentation formatting at all, and it must be done manually.
Short answer, you can't (without some hack), but, the default is consistent in this case, meaning: Xcode does indent, the way it knows how to. IMHO, it won't be a bad idea to adapt it.
Related
I'm trying to copy my "DescriptionLabel" to the Pasteboard. DescriptionLabel is set as a UITextView (I know the name is a little confusing...). Anyway,
- (IBAction)copy:(id)sender {
UIPasteboard *appPasteBoard = [UIPasteboard generalPasteboard];
appPasteBoard.persistent = YES;
[appPasteBoard setString:#"This text is being copied"];
}
The string in the code is being copied, but I can't manage to get it copying my UITextView/DescriptionLabel. This:
[appPasteBoard setString:_DescriptionLabel];
is not working.
Do any of you have any clue of what I can do to make it work? Been struggling with this for days...
Well, the problem is that you are using setString: to store UITextView, which is an UIKit control and not NSString, in pasteboard. What you probably mean is to store its text value.
Objective-C does not support implicit conversions like Scala or Swift. Solution is simple, just access the text property explicitly:
[appPasteBoard setString:_DescriptionLabel.text];
I encourage you to look into UIPasteboard documentation for details concerning it API: https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIPasteboard_Class/index.html
I am currently doing the CS193P lessons via iTunesU and the teacher mentioned the Build and Analyze option several times. He said that it was a nice tool and fun to play with.
So I tried, and noticed that it doesn't work, or that I don't understand how it should work (I think the last option).
I have a few memory leaks, and it is not warning me at all! I saw online that a blue thing should appear telling me it is a leak, but I don't see anything although I'm doing NSDictionary *dict = [[NSDictionary alloc] init];.
How is it supposed to work? From what I read on the internet I thought it should signal potential leaks. What am I doing wrong?
I'm using XCode 3.2.5.
Thanks.
Update:
This is a kind of bug, I think.
When I declare this in the interface like NSDictionary *dict; and initialize it (but nowhere deallocating it) it says nothing.
When I declare and initialize it in - (void) init and don't release it in there like:
- (void) init {
if(self = [super init])
NSDictionary *dict = [[NSDictionary alloc] init];
return self;
}
It does signal a leak. Why? Is this because of my settings? Is this a bug? If it is a bug, where and how should I report it?
It's giving you a warning because you're not deallocating it.
-(void)dealloc{
[super dealloc];
[dict dealloc];
}
It's not warning you because you should be able to release the objects as soon as you create them, and the analyzer goal is to alert you on possible leaks in your code.
You can either use autorelease, or you dealloc the object you create manually.
P.S., little curiosity: why are you using Xcode 3.2.5?
Don't know exactly if that version can, but in the latest versions of Xcode, when you run that tool, you are able to see WHAT object you are deallocating with the means of some arrows with explanation, something like
I just found out that a reboot and restart of Xcode will bring it back.
Thanks for the help.
I just need to paste the contents of the clipboard/pasteboard to an NSTextView by way of an action.
NSString *PboardType = #"PboardType";
- (IBAction)paste:sender
{
NSPasteboard *pb = [NSPasteboard generalPasteboard];
NSData *copiedData = [pb dataForType:PboardType];
[self RTFFromRange:copiedData];
}
The last line is obviously wrong. what do I need to do?
Thanks
Paul
Nothing. An NSTextView already supports pasting (and cutting and copying, and fonts, and search, and spelling-checking, and …). You don't need to do anything to implement it.
If you're implementing some different paste behavior, you should edit your question to specify what special behavior you're trying to achieve (as well as why the user will want your application to do something unusual).
If the standard paste behavior isn't working, you should edit your question to specify how it isn't working.
I have the Apress "Learn Cocoa" book (published in 2010 BTW) and I am getting a deprecation error on one of the lines. The code is:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
self.villain = [[NSMutableDictionary alloc] initWithObjectsAndKeys:#"Lex Luthor", kName, #"Smallville", kLastKnownLocation, [NSDate date], kLastSeenDate, #"Superman", kSwornEnemy, #"Revenge", kPrimaryMotivation, [NSArray arrayWithObjects:#"Intellect", #"Leadership", nil], kPowers, #"Superhero Action", kPowerSource, [NSNumber numberWithInt:9], kEvilness, [NSImage imageNamed:#"NSUser"], kMugshot, #"", kNotes, nil];
self.villains = [NSMutableArray arrayWithObject:self.villain];
[villainsTableView reloadData];
[villainsTableView selectRow:0 byExtendingSelection:NO];
[self updateDetailViews];
}
I am getting the error on the 2nd to last line and I don't know exactly what that line is intending to do.
The replacement for the deprecated method would look like this:
[villainsTableView selectRowIndexes:[NSIndexSet indexSetWithIndex:0] byExtendingSelection:NO];
The documentation tells you what that message does and what the replacement is.
In XCode you can place the mousecursor above the symbol you want to know something about. While placing the mousecursor above this symbol (i.e. NSString), hold down the ALT-Key and doubleclick. This will open up a context sensitive documentation. All deprecated methods and symbols are marked red there. Mostly the new replacement is documented next to the old one. In the new xcode, a popup-window will appear. The doc will be opened by clicking the "notebook"-icon at the border of the box.
i.e. for NSString you will find:
– initWithCString: Deprecated in iOS 2.0
+ stringWithCString:encoding:
With an educated guess you will choose "+ stringWithCString:encoding:"
Apple almost adds new functionality which is a logical augmentation of the old stuff, so you don't need to google hard, but watch into the method summary of the related doc.
I have a list view class that just like NSCollectionView requires an additional prototype item and a prototype view to be of any use.
When dropping an NSCollectionView from the library in Interface Builder those two helper items are automatically created. However I couldn't find a single official Apple document dealing with this use case (describing how its done).
Digging thru the Apple Dev Guides I could however find "ibDidAddToDesignableDocument:".
With the following code I managed to get my auxiliary items created on drop from library:
- (void)ibDidAddToDesignableDocument:(IBDocument *)document {
[super ibDidAddToDesignableDocument:document];
NSView *prototypeView = [[[NSView alloc] initWithFrame:NSMakeRect(0.0, 0.0, 300, 65.0)] autorelease];
DLListViewItem *prototypeViewItem = [[[DLListViewItem alloc] initWithNibName:nil bundle:nil] autorelease];
[document addObject:prototypeViewItem toParent:nil];
[document addObject:prototypeView toParent:nil];
[document connectOutlet:#"view" ofSourceObject:prototypeViewItem toDestinationObject:prototypeView];
[document connectOutlet:#"listView" ofSourceObject:prototypeViewItem toDestinationObject:self];
[document connectOutlet:#"prototypeItem" ofSourceObject:self toDestinationObject:prototypeViewItem];
}
However…
…IB adds those aux items for NSCollectionView only on the actual initial drag from the library, not on any other call of "ibDidAddToDesignableDocument:", such as when embedding, copying or duplicating the item. (while my method would, and on all)
This makes me wonder whether Apple actually uses "ibDidAddToDesignableDocument:" for this and if I'm on the right track with this at all.
How does one imitate this properly? I'm having a hard time trying to distinguish between different contexts for "ibDidAddToDesignableDocument:". Anybody successfully done this?
Unfortunately none of Google, Google Code, GitHub, or the documentation revealed anything helpful, so I'm in desperate need of help here. :(
Thanks in advance!
Edit: Oh great, this question just brought me the tumbleweed badge, yay! Not.
I'm more into useful answers actually, but thanks anyway ;)
I struggled with this on a plugin I did myself a while ago. In my case I was able to check a property of the object to see if it had been initialized already and skip adding the auxilliary objects in that case. I believe BWToolkit uses some internal checking that is similar. Couldn't you check your object's 'prototypeItem' property to see if you need to skip creating your aux objects?