What's the Rationale of Swift's Size Methods Taking 'Int'S

Swift: How to use sizeof?

Use sizeof as follows:

let size = sizeof(Int)

sizeof uses the type as the parameter.

If you want the size of the anInt variable you can pass the dynamicType field to sizeof.

Like so:

var anInt: Int = 5
var anIntSize: Int = sizeof(anInt.dynamicType)

Or more simply (pointed out by user102008):

var anInt: Int = 5
var anIntSize: Int = sizeofValue(anInt)

Confused about UIImage's init(data:scale:) method in Swift 3.0

The init method you are trying to use is not for the purpose of resizing an image. And the scale parameter is not a resizing scale. It's the 1x, 2x, 3x scale. Essentially, the only valid values for scale are currently 1.0, 2.0, or 3.0.

While setting the scale to the inverse of what you expected gives you a size property returning your desired result, it is not at all what you should be doing.

There are proper ways to resize an image such as How to Resize image in Swift? as well as others.

What data type should I use when converting a byte array from Swift to Objective-C

The Swift type [Byte] is not representable in Objective-C, but you can work with
a pointer to the bytes:

func writeBytes(data: ConstUnsafePointer<Byte>, length: Int) { ... }

var data : [Byte] = [1, 2, 3]
writeBytes(data, length: data.count)

This mapped to Objective-C as

- (void)writeBytes:(Byte const *)data length:(NSInteger)length;

so that you can call it like

Byte data[] = {1, 2, 3};
[buffer writeBytes:data length:3];

When to use NSInteger vs. int

You usually want to use NSInteger when you don't know what kind of processor architecture your code might run on, so you may for some reason want the largest possible integer type, which on 32 bit systems is just an int, while on a 64-bit system it's a long.

I'd stick with using NSInteger instead of int/long unless you specifically require them.

NSInteger/NSUInteger are defined as *dynamic typedef*s to one of these types, and they are defined like this:

#if __LP64__ || TARGET_OS_EMBEDDED || TARGET_OS_IPHONE || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64
typedef long NSInteger;
typedef unsigned long NSUInteger;
#else
typedef int NSInteger;
typedef unsigned int NSUInteger;
#endif

With regard to the correct format specifier you should use for each of these types, see the String Programming Guide's section on Platform Dependencies

What is int foo[] = {1200,100} and how do I recreate it in Swift?

What am I looking at here?

That is an array of ints, with two elements.


[How can I] create the same variable in Swift?

The same variable in swift might be declared as:

var beepData : [Int] = [ 1200, 100 ]

You might find this answer about different ways to declare an array in C useful


What is (int *)

It's an int pointer, it points to the memory address of an int. Incrementing it would move along the memory addresses (in int-sized chunks) and point to the next bit of memory.

[1][3][5][4][2]
^

This little arrow represents an int*. Even though it currently points to 1,
incrementing it doesn't equal 2. In this case it would equal 3, the value of the int in the next block of memory.

[1][3][5][4][2]
^

How might I create one in Swift?

To be quite honest, I'm not sure if Swift has pointers in the normal sense. I've not used it a great deal. However, if you are porting that method, I'd probably give it an array of ints.

func barcodeSetScanBeep(enabled : Bool, volume : Int, beepData: [Int], length : Int, error : NSError)

How to write method to calculate average in Swift-playground

It's so much easier with just a straightforward call to reduce:

let array = [1.0,2.0,3.0]
var average = array.reduce(0.0) {
return $0 + $1/Double(array.count)
}
// average = 2.0

How to set UICollectionViewCell Width and Height programmatically

Use this method to set custom cell height width.

Make sure to add this protocols

UICollectionViewDelegate

UICollectionViewDataSource

UICollectionViewDelegateFlowLayout

If you are using swift 5 or xcode 11 and later you need to set Estimate Size to none using storyboard in order to make it work properly. If you will not set that than below code will not work as expected.

Sample Image

Swift 4 or Later

extension YourViewController: UICollectionViewDelegate {
//Write Delegate Code Here
}

extension YourViewController: UICollectionViewDataSource {
//Write DataSource Code Here
}

extension YourViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: screenWidth, height: screenWidth)
}
}

Objective-C

@interface YourViewController : UIViewController<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(CGRectGetWidth(collectionView.frame), (CGRectGetHeight(collectionView.frame)));
}


Related Topics



Leave a reply



Submit