Objective-c renaming files




















The current working directory may be identified using the currentDirectoryPath instance method of our NSFileManager object. The current path is returned from the method in the form of an NSString object:. The current working directory of a running Objective-C program can be changed with a call to the changeCurrentDirectoryPath method.

The destination directory path is passed as an argument to the instance method in the form of an NSString object. Note that this method returns a boolean YES or NO result to indicate if the requested directory change was successful for not:.

A new directory is created using the createDirectoryAtURL instance method, this time passing through the through the pathname of the new directory as an argument in the form of an NSURL object. This method also takes additional arguments in the form of a set of attributes for the new directory and a Boolean value indicating whether or not intermediate directories should be created if they do not already exist.

Specifying nil will use the default attributes:. An existing directory may be removed from the file system using the removeItemAtPath method, passing though the path of the directory to be removed as an argument:. This method takes the source and destination pathnames as arguments in the form of NSURL objects and requires that the destination path not already exist.

If the target exists, a boolean NO result is returned by the method to indicate failure of the operation:. Because Objective-C does not provide namespacing, non-static functions should have a prefix that minimizes the chance of a name collision. Instance variables have leading underscores. File scope or global variables have a prefix g. Readers should be able to infer the variable type from the name, but do not use Hungarian notation for syntactic attributes, such as the static type of a variable int or pointer.

File scope or global variables as opposed to constants declared outside the scope of a method or function should be rare, and should have the prefix g. Existing projects may opt to continue using trailing underscores in new code in order to maintain consistency within the project codebase.

Consistency of prefix or suffix underscores should be maintained within each class. Constant symbols const global and static variables and constants created with define should use mixed case to delimit words. Because Objective-C does not provide namespacing, constants with external linkage should have a prefix that minimizes the chance of a name collision, typically like ClassNameConstantName or ClassNameEnumName.

For interoperability with Swift code, enumerated values should have names that extend the typedef name:. A lowercase k can be used as a standalone prefix for constants of static storage duration declared within implementation files:. NOTE: Previous convention was for public constant names to begin with a lowercase k followed by a project-specific prefix.

This practice is no longer recommended. As shown in the example , the recommended order for declarations in an interface declaration are: properties, class methods, initializers, and then finally instance methods. The class methods section should begin with any convenience constructors. Declare variables in the narrowest practical scopes, and close to their use.

Initialize variables in their declarations. Occasionally, efficiency will make it more appropriate to declare a variable outside the scope of its use. This example declares meters separate from initialization, and needlessly sends the lastKnownLocation message each time through the loop:.

Under Automatic Reference Counting, strong and weak pointers to Objective-C objects are automatically initialized to nil , so explicit initialization to nil is not required for those common cases.

When in doubt, prefer to initialize all Objective-C local variables. Subtle errors crop up when doing math or counting down to zero using unsigned integers.

Rely only on signed integers in math expressions except when matching NSUInteger in system interfaces. Use of these types is appropriate when handling values exposed by system interfaces, but they should be avoided for most other computations. Comments are absolutely vital to keeping our code readable. The following rules describe what you should comment and where. But remember: while comments are important, the best code is self-documenting.

Giving sensible names to types and variables is much better than using obscure names and then trying to explain them through comments. Pay attention to punctuation, spelling, and grammar; it is easier to read well-written comments than badly written ones. Comments should be as readable as narrative text, with proper capitalization and punctuation. In many cases, complete sentences are more readable than sentence fragments. Shorter comments, such as comments at the end of a line of code, can sometimes be less formal, but use a consistent style.

When writing your comments, write for your audience: the next contributor who will need to understand your code. Be generous—the next one may be you! A file may optionally start with a description of its contents. Every file may contain the following items, in order:. If you make significant changes to a file with an author line, consider deleting the author line since revision history already provides a more detailed and accurate record of authorship.

Every non-trivial interface, public and private, should have an accompanying comment describing its purpose and how it fits into the larger picture. Comments should be used to document classes, properties, ivars, functions, categories, protocol declarations, and enums. Doxygen-style comments are encouraged for interfaces as they are parsed by Xcode to display formatted documentation. There is a wide variety of Doxygen commands; use them consistently within a project.

Additionally, each method should have a comment explaining its function, arguments, return value, thread or queue assumptions, and any side effects. Documentation comments should be in the header for public methods, or immediately preceding the method for non-trivial private methods.

The comment describes the function; it does not tell the function what to do. Document the thread usage assumptions the class, properties, or methods make, if any. If an instance of the class can be accessed by multiple threads, take extra care to document the rules and invariants surrounding multithreaded use. Any sentinel values for properties and ivars, such as NULL or -1 , should be documented in comments. Declaration comments explain how a method or function is used. Comments explaining how a method or function is implemented should be with the implementation rather than with the declaration.

When useful, also provide comments about implementation approaches that were considered or abandoned. End-of-line comments should be separated from the code by at least 2 spaces. If you have several comments on subsequent lines, it can often be more readable to line them up.

Where needed to avoid ambiguity, use backticks or vertical bars to quote variable names and symbols in comments in preference to using quotation marks or naming the symbols inline. In Doxygen-style comments, prefer demarcating symbols with a monospace text command, such as c. Demarcation helps provide clarity when a symbol is a common word that might make the sentence read like it was poorly constructed. A common example is the symbol count :.

For objects not managed by ARC, make the pointer ownership model as explicit as possible when it falls outside the most common Objective-C usage idioms. An exception is in Mac software for instance variables labeled as IBOutlets , which are presumed to not be retained.

Core Foundation and other non-Objective-C object pointers require explicit memory management, even when building for automatic reference counting. Object ownership and lifetime are explicit when using ARC, so no additional comments are required for automatically retained objects. Avoid macros, especially where const variables, enums, XCode snippets, or C functions may be used instead.

Macros make the code you see different from the code the compiler sees. Modern C renders traditional uses of macros for constants and utility functions unnecessary. Macros should only be used when there is no other solution available. Where a macro is needed, use a unique name to avoid the risk of a symbol collision in the compilation unit.

If practical, keep the scope limited by undefining the macro after its use. Function-like macros may use C function naming practices. Do not define macros that appear to be C or Objective-C keywords.

Avoid macros that expand to unbalanced C or Objective-C constructs. Avoid macros that introduce scope, or may obscure the capturing of values in blocks. Avoid macros that generate class, property, or method definitions in headers to be used as public API. These only make the code hard to understand, and the language already has better ways of doing this. Stack Overflow for Teams — Collaborate and share knowledge with a private group.

Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Renaming an existing file with Obj-C Ask Question. Asked 9 years ago. Active 9 years ago. Viewed 9k times. Daij-Djan 48k 17 17 gold badges silver badges bronze badges. Charles Lillo Charles Lillo 3 3 silver badges 16 16 bronze badges. So you want to rename newPDF. Looking to do just that. Add a comment. Active Oldest Votes. CubeSchrauber CubeSchrauber 1, 8 8 silver badges 9 9 bronze badges.



0コメント

  • 1000 / 1000